add related info about deadlock and ipc: pipe, signal
This commit is contained in:
parent
e378ef4376
commit
026015ed25
243
related_info/lab7/deadlock/bankers-homework.py
Normal file
243
related_info/lab7/deadlock/bankers-homework.py
Normal file
@ -0,0 +1,243 @@
|
||||
import os
|
||||
import random
|
||||
import numpy as np
|
||||
import itertools
|
||||
|
||||
class Bankers(object):
|
||||
def __init__(self, totalResource):
|
||||
#initiating
|
||||
self.RESOURCE = totalResource
|
||||
|
||||
def SignProcesses(self, max_, allocated_):
|
||||
self.max = max_
|
||||
self.allocated = allocated_
|
||||
self.need = self.CalcNeed()
|
||||
self.avaliable = self.CalcAvaliable()
|
||||
self.finished = [False]*len(self.allocated)
|
||||
|
||||
def Difference(self,a,b):
|
||||
#return matrix subtracted from a by b
|
||||
res = []
|
||||
for i in range(len(a)):
|
||||
tmp = []
|
||||
for j in range(len(a[i])):
|
||||
tmp.append(a[i][j]-b[i][j])
|
||||
res.append(tmp)
|
||||
return res
|
||||
|
||||
def CalcNeed(self):
|
||||
#calc request by subtracting signed matrix from max matrix
|
||||
return self.Difference(self.max,self.allocated)
|
||||
|
||||
def CalcAvaliable(self):
|
||||
"""Calc Avaliable Resource"""
|
||||
a = self.allocated
|
||||
res = []
|
||||
for j in range(len(a[0])):
|
||||
tmp = 0
|
||||
for i in range(len(a)):
|
||||
tmp += a[i][j]
|
||||
res.append(self.RESOURCE[j] - tmp)
|
||||
return res
|
||||
|
||||
def ExecuteProcess(self,index):
|
||||
|
||||
#check if less avaliable than Request
|
||||
# YOUR CODE, YOUR ID
|
||||
#check END here
|
||||
|
||||
#allocating what they need.
|
||||
# YOUR CODE, YOUR ID
|
||||
#allocating END here
|
||||
pass
|
||||
|
||||
def TempSafeCheckAfterRelease(self):
|
||||
#check if at least one request can be done after previous process done. not check whole sequances.
|
||||
#if every element of Requests can't accepted after previous process done, this mean it is not safe state
|
||||
# YOUR CODE, YOU ID
|
||||
#check END here
|
||||
pass
|
||||
|
||||
def print_matrixes(self):
|
||||
print "_____________________________________________"
|
||||
print "MAX\t\tAllocated\tNeed"
|
||||
for idx in range(len(self.max)):
|
||||
print "%s\t%s\t%s" % (self.max[idx],self.allocated[idx], self.need[idx])
|
||||
print "_____________________________________________"
|
||||
print "Resources:"
|
||||
print "Total: %s\tAvailable: %s\n" % (self.RESOURCE, self.avaliable)
|
||||
|
||||
def ReleasingProcess(self,index):
|
||||
for i in range(0,len(self.RESOURCE)):
|
||||
self.finished[index] = True
|
||||
self.allocated[index][i] = 0
|
||||
self.avaliable = self.CalcAvaliable()
|
||||
|
||||
def Execute(self):
|
||||
i = 0
|
||||
# get all permutation of processes
|
||||
perm = itertools.permutations(range(procnum), procnum)
|
||||
permArray = np.asarray(list(perm))
|
||||
|
||||
for arr in permArray:
|
||||
for i in arr:
|
||||
if self.finished[i] == False:
|
||||
print "Executing..."
|
||||
print "Request: "
|
||||
print self.need[i]
|
||||
#check if less avaliable than Request
|
||||
if self.ExecuteProcess(i):
|
||||
print "Dispatching Done..."
|
||||
|
||||
self.print_matrixes()
|
||||
|
||||
print "-----Releasing Process------"
|
||||
|
||||
self.ReleasingProcess(i)
|
||||
|
||||
self.print_matrixes()
|
||||
|
||||
#check if at least one request can be done after previous process done. not check whole sequances.
|
||||
#if every element of Requests can't accepted after previous process done, this mean it is not safe state
|
||||
if not (self.TempSafeCheckAfterRelease()):
|
||||
print "SAFE STATE: NOT SAFE - There are no sequances can avoid Deadlock"
|
||||
return False
|
||||
processes.append(i)
|
||||
else:
|
||||
print "HOLD: not enough Resource"
|
||||
|
||||
if i == len(self.allocated)-1:
|
||||
i = 0
|
||||
else:
|
||||
i += 1
|
||||
|
||||
check = True
|
||||
for k in range(0,len(self.allocated)):
|
||||
if self.finished[k] == False:
|
||||
check = False
|
||||
break
|
||||
if check == True:
|
||||
return True
|
||||
break
|
||||
#every permutation of processes is false
|
||||
return False
|
||||
|
||||
def getmax():
|
||||
res = []
|
||||
for j in range(procnum):
|
||||
tmp = []
|
||||
for i in range(len(total_resources)):
|
||||
randnum=random.random()
|
||||
remain_max=0
|
||||
if j >0:
|
||||
remain_max=total_resources[i]
|
||||
for k in range(j):
|
||||
remain_max=remain_max-res[k][i]
|
||||
if remain_max < 0:
|
||||
remain_max=0
|
||||
else:
|
||||
remain_max=total_resources[i]
|
||||
tmp.append((int)(randnum*remain_max*0.8))
|
||||
res.append(tmp)
|
||||
return res
|
||||
|
||||
def getallocated():
|
||||
res = []
|
||||
for j in range(procnum):
|
||||
tmp = []
|
||||
for i in range(len(total_resources)):
|
||||
randnum=random.random()
|
||||
remain=0
|
||||
if j >0:
|
||||
remain=max[j][i]
|
||||
for k in range(j):
|
||||
remain=remain-res[k][i]
|
||||
if remain < 0:
|
||||
remain=0
|
||||
else:
|
||||
remain=max[j][i]
|
||||
tmp.append((int)(randnum*remain))
|
||||
res.append(tmp)
|
||||
return res
|
||||
|
||||
print "start here"
|
||||
# random seed
|
||||
seed = 2
|
||||
random.seed(seed)
|
||||
# the number of process list
|
||||
procnum = 3
|
||||
# the number of type of resource
|
||||
resnum = 4
|
||||
# the max total value of resource
|
||||
restotalval = 30
|
||||
# the total resources list
|
||||
total_resources=[]
|
||||
# the total processes
|
||||
processes=[]
|
||||
# set the real total value of resource in total_resources
|
||||
for i in range(resnum):
|
||||
total_resources.append((int)(restotalval*random.random()))
|
||||
# init the Banker
|
||||
b = Bankers(total_resources)
|
||||
# get the max request values of resources from process
|
||||
max=getmax()
|
||||
# get the already gotted values of resources from process
|
||||
allocated=getallocated()
|
||||
# init need matrix, available vector
|
||||
b.SignProcesses(max, allocated)
|
||||
# print all theses matrixes
|
||||
b.print_matrixes()
|
||||
# executing Banker algorithm
|
||||
result=b.Execute()
|
||||
# show results
|
||||
if result:
|
||||
print "SUCCESS proc lists ",processes
|
||||
else:
|
||||
print "Failed"
|
||||
|
||||
# total_resources = [6, 5, 7, 6]
|
||||
# processes=[]
|
||||
# b = Bankers(total_resources)
|
||||
#
|
||||
# max = [
|
||||
# [3, 3, 2, 2],
|
||||
# [1, 2, 3, 4],
|
||||
# [1, 3, 5, 0],
|
||||
# ]
|
||||
# allocated = [
|
||||
# [1, 2, 2, 1],
|
||||
# [1, 0, 3, 3],
|
||||
# [1, 2, 1, 0],
|
||||
# ]
|
||||
#
|
||||
# b.SignProcesses(max, allocated)
|
||||
# b.print_matrixes()
|
||||
# result=b.Execute()
|
||||
# if result:
|
||||
# print "SUCCESS proc lists ",processes
|
||||
# else:
|
||||
# print "Failed"
|
||||
#
|
||||
#
|
||||
# total_resources = [10, 10, 8, 5]
|
||||
# processes=[]
|
||||
# b = Bankers(total_resources)
|
||||
# max = [
|
||||
# [10, 8, 2,5],
|
||||
# [6, 1, 3,1],
|
||||
# [3, 1, 4,2],
|
||||
# [5, 4, 2,1]
|
||||
# ]
|
||||
# allocated = [
|
||||
# [3, 0, 0,3],
|
||||
# [1, 1, 2,0],
|
||||
# [2, 1, 2,1],
|
||||
# [0, 0, 2,0]
|
||||
# ]
|
||||
# b.SignProcesses(max, allocated)
|
||||
# b.print_matrixes()
|
||||
# result=b.Execute()
|
||||
# if result:
|
||||
# print "SUCCESS proc lists ",processes
|
||||
# else:
|
||||
# print "Failed"
|
257
related_info/lab7/deadlock/example-output.txt
Normal file
257
related_info/lab7/deadlock/example-output.txt
Normal file
@ -0,0 +1,257 @@
|
||||
start here
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[18, 16, 0, 0] [17, 8, 0, 0] [1, 8, 0, 0]
|
||||
[4, 5, 0, 0] [0, 0, 0, 0] [4, 5, 0, 0]
|
||||
[2, 2, 0, 1] [0, 0, 0, 0] [2, 2, 0, 1]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [28, 28, 1, 2] Available: [11, 20, 1, 2]
|
||||
|
||||
Executing...
|
||||
Request:
|
||||
[1, 8, 0, 0]
|
||||
Dispatching Done...
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[18, 16, 0, 0] [18, 16, 0, 0] [0, 0, 0, 0]
|
||||
[4, 5, 0, 0] [0, 0, 0, 0] [4, 5, 0, 0]
|
||||
[2, 2, 0, 1] [0, 0, 0, 0] [2, 2, 0, 1]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [28, 28, 1, 2] Available: [10, 12, 1, 2]
|
||||
|
||||
-----Releasing Process------
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[18, 16, 0, 0] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[4, 5, 0, 0] [0, 0, 0, 0] [4, 5, 0, 0]
|
||||
[2, 2, 0, 1] [0, 0, 0, 0] [2, 2, 0, 1]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [28, 28, 1, 2] Available: [28, 28, 1, 2]
|
||||
|
||||
Executing...
|
||||
Request:
|
||||
[4, 5, 0, 0]
|
||||
Dispatching Done...
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[18, 16, 0, 0] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[4, 5, 0, 0] [4, 5, 0, 0] [0, 0, 0, 0]
|
||||
[2, 2, 0, 1] [0, 0, 0, 0] [2, 2, 0, 1]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [28, 28, 1, 2] Available: [24, 23, 1, 2]
|
||||
|
||||
-----Releasing Process------
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[18, 16, 0, 0] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[4, 5, 0, 0] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[2, 2, 0, 1] [0, 0, 0, 0] [2, 2, 0, 1]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [28, 28, 1, 2] Available: [28, 28, 1, 2]
|
||||
|
||||
Executing...
|
||||
Request:
|
||||
[2, 2, 0, 1]
|
||||
Dispatching Done...
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[18, 16, 0, 0] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[4, 5, 0, 0] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[2, 2, 0, 1] [2, 2, 0, 1] [0, 0, 0, 0]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [28, 28, 1, 2] Available: [26, 26, 1, 1]
|
||||
|
||||
-----Releasing Process------
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[18, 16, 0, 0] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[4, 5, 0, 0] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[2, 2, 0, 1] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [28, 28, 1, 2] Available: [28, 28, 1, 2]
|
||||
|
||||
SUCCESS proc lists [0, 1, 2]
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[3, 3, 2, 2] [1, 2, 2, 1] [2, 1, 0, 1]
|
||||
[1, 2, 3, 4] [1, 0, 3, 3] [0, 2, 0, 1]
|
||||
[1, 3, 5, 0] [1, 2, 1, 0] [0, 1, 4, 0]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [6, 5, 7, 6] Available: [3, 1, 1, 2]
|
||||
|
||||
Executing...
|
||||
Request:
|
||||
[2, 1, 0, 1]
|
||||
Dispatching Done...
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[3, 3, 2, 2] [3, 3, 2, 2] [0, 0, 0, 0]
|
||||
[1, 2, 3, 4] [1, 0, 3, 3] [0, 2, 0, 1]
|
||||
[1, 3, 5, 0] [1, 2, 1, 0] [0, 1, 4, 0]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [6, 5, 7, 6] Available: [1, 0, 1, 1]
|
||||
|
||||
-----Releasing Process------
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[3, 3, 2, 2] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[1, 2, 3, 4] [1, 0, 3, 3] [0, 2, 0, 1]
|
||||
[1, 3, 5, 0] [1, 2, 1, 0] [0, 1, 4, 0]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [6, 5, 7, 6] Available: [4, 3, 3, 3]
|
||||
|
||||
Executing...
|
||||
Request:
|
||||
[0, 2, 0, 1]
|
||||
Dispatching Done...
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[3, 3, 2, 2] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[1, 2, 3, 4] [1, 2, 3, 4] [0, 0, 0, 0]
|
||||
[1, 3, 5, 0] [1, 2, 1, 0] [0, 1, 4, 0]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [6, 5, 7, 6] Available: [4, 1, 3, 2]
|
||||
|
||||
-----Releasing Process------
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[3, 3, 2, 2] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[1, 2, 3, 4] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[1, 3, 5, 0] [1, 2, 1, 0] [0, 1, 4, 0]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [6, 5, 7, 6] Available: [5, 3, 6, 6]
|
||||
|
||||
Executing...
|
||||
Request:
|
||||
[0, 1, 4, 0]
|
||||
Dispatching Done...
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[3, 3, 2, 2] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[1, 2, 3, 4] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[1, 3, 5, 0] [1, 3, 5, 0] [0, 0, 0, 0]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [6, 5, 7, 6] Available: [5, 2, 2, 6]
|
||||
|
||||
-----Releasing Process------
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[3, 3, 2, 2] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[1, 2, 3, 4] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[1, 3, 5, 0] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [6, 5, 7, 6] Available: [6, 5, 7, 6]
|
||||
|
||||
SUCCESS proc lists [0, 1, 2]
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[10, 8, 2, 5] [3, 0, 0, 3] [7, 8, 2, 2]
|
||||
[6, 1, 3, 1] [1, 1, 2, 0] [5, 0, 1, 1]
|
||||
[3, 1, 4, 2] [2, 1, 2, 1] [1, 0, 2, 1]
|
||||
[5, 4, 2, 1] [0, 0, 2, 0] [5, 4, 0, 1]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [10, 10, 8, 5] Available: [4, 8, 2, 1]
|
||||
|
||||
Executing...
|
||||
Request:
|
||||
[7, 8, 2, 2]
|
||||
HOLD: not enough Resource
|
||||
Executing...
|
||||
Request:
|
||||
[5, 0, 1, 1]
|
||||
HOLD: not enough Resource
|
||||
Executing...
|
||||
Request:
|
||||
[1, 0, 2, 1]
|
||||
Dispatching Done...
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[10, 8, 2, 5] [3, 0, 0, 3] [7, 8, 2, 2]
|
||||
[6, 1, 3, 1] [1, 1, 2, 0] [5, 0, 1, 1]
|
||||
[3, 1, 4, 2] [3, 1, 4, 2] [0, 0, 0, 0]
|
||||
[5, 4, 2, 1] [0, 0, 2, 0] [5, 4, 0, 1]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [10, 10, 8, 5] Available: [3, 8, 0, 0]
|
||||
|
||||
-----Releasing Process------
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[10, 8, 2, 5] [3, 0, 0, 3] [7, 8, 2, 2]
|
||||
[6, 1, 3, 1] [1, 1, 2, 0] [5, 0, 1, 1]
|
||||
[3, 1, 4, 2] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[5, 4, 2, 1] [0, 0, 2, 0] [5, 4, 0, 1]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [10, 10, 8, 5] Available: [6, 9, 4, 2]
|
||||
|
||||
Executing...
|
||||
Request:
|
||||
[7, 8, 2, 2]
|
||||
HOLD: not enough Resource
|
||||
Executing...
|
||||
Request:
|
||||
[5, 0, 1, 1]
|
||||
Dispatching Done...
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[10, 8, 2, 5] [3, 0, 0, 3] [7, 8, 2, 2]
|
||||
[6, 1, 3, 1] [6, 1, 3, 1] [0, 0, 0, 0]
|
||||
[3, 1, 4, 2] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[5, 4, 2, 1] [0, 0, 2, 0] [5, 4, 0, 1]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [10, 10, 8, 5] Available: [1, 9, 3, 1]
|
||||
|
||||
-----Releasing Process------
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[10, 8, 2, 5] [3, 0, 0, 3] [7, 8, 2, 2]
|
||||
[6, 1, 3, 1] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[3, 1, 4, 2] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[5, 4, 2, 1] [0, 0, 2, 0] [5, 4, 0, 1]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [10, 10, 8, 5] Available: [7, 10, 6, 2]
|
||||
|
||||
Executing...
|
||||
Request:
|
||||
[7, 8, 2, 2]
|
||||
Dispatching Done...
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[10, 8, 2, 5] [10, 8, 2, 5] [0, 0, 0, 0]
|
||||
[6, 1, 3, 1] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[3, 1, 4, 2] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[5, 4, 2, 1] [0, 0, 2, 0] [5, 4, 0, 1]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [10, 10, 8, 5] Available: [0, 2, 4, 0]
|
||||
|
||||
-----Releasing Process------
|
||||
_____________________________________________
|
||||
MAX Allocated Need
|
||||
[10, 8, 2, 5] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[6, 1, 3, 1] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[3, 1, 4, 2] [0, 0, 0, 0] [0, 0, 0, 0]
|
||||
[5, 4, 2, 1] [0, 0, 2, 0] [5, 4, 0, 1]
|
||||
_____________________________________________
|
||||
Resources:
|
||||
Total: [10, 10, 8, 5] Available: [10, 10, 6, 5]
|
||||
|
||||
Failed
|
23
related_info/lab7/ipc/pipe-ex1.py
Normal file
23
related_info/lab7/ipc/pipe-ex1.py
Normal file
@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- encoding: utf8 -*-
|
||||
import os, sys
|
||||
print "I'm going to fork now - the child will write something to a pipe, and the parent will read it back"
|
||||
r, w = os.pipe() # r,w是文件描述符, 不是文件对象
|
||||
pid = os.fork()
|
||||
if pid:
|
||||
# 父进程
|
||||
os.close(w) # 关闭一个文件描述符
|
||||
r = os.fdopen(r) # 将r转化为文件对象
|
||||
print "parent: reading"
|
||||
txt = r.read()
|
||||
os.waitpid(pid, 0) # 确保子进程被撤销
|
||||
else:
|
||||
# 子进程
|
||||
os.close(r)
|
||||
w = os.fdopen(w, 'w')
|
||||
print "child: writing"
|
||||
w.write("here's some text from the child")
|
||||
w.close()
|
||||
print "child: closing"
|
||||
sys.exit(0)
|
||||
print "parent: got it; text =", txt
|
43
related_info/lab7/ipc/pipe-ex2.c
Normal file
43
related_info/lab7/ipc/pipe-ex2.c
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* From
|
||||
* [url]http://www.crasseux.com/books/ctutorial/Programming-with-pipes.html[/url]
|
||||
* but changed to use fgets() instead of the GNU extension getdelim()
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
int main()
|
||||
{
|
||||
FILE *ps_pipe;
|
||||
FILE *grep_pipe;
|
||||
int bytes_read;
|
||||
char buffer[100]; /* could be anything you want */
|
||||
/* Open our two pipes
|
||||
ls -a | grep pipe*
|
||||
*/
|
||||
|
||||
ps_pipe = popen("/bin/ls -a", "r");
|
||||
grep_pipe = popen("/bin/grep 'pipe*'", "w");
|
||||
/* Check that pipes are non-null, therefore open */
|
||||
if ((!ps_pipe) || (!grep_pipe)) {
|
||||
fprintf(stderr, "One or both pipes failed.\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
bytes_read = 0;
|
||||
while (fgets(buffer, sizeof(buffer), ps_pipe)) {
|
||||
fprintf(grep_pipe, "%s", buffer);
|
||||
bytes_read += strlen(buffer);
|
||||
}
|
||||
printf("Total bytes read = %d\n", bytes_read);
|
||||
/* Close ps_pipe, checking for errors */
|
||||
if (pclose(ps_pipe) != 0) {
|
||||
fprintf(stderr, "Could not run 'ls', or other error.\n");
|
||||
}
|
||||
/* Close grep_pipe, cehcking for errors */
|
||||
if (pclose(grep_pipe) != 0) {
|
||||
fprintf(stderr, "Could not run 'grep', or other error.\n");
|
||||
}
|
||||
/* Exit! */
|
||||
return 0;
|
||||
}
|
||||
|
28
related_info/lab7/ipc/signal-ex1.c
Normal file
28
related_info/lab7/ipc/signal-ex1.c
Normal file
@ -0,0 +1,28 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
|
||||
// Define the function to be called when ctrl-c (SIGINT) signal is sent to process
|
||||
void
|
||||
signal_callback_handler(int signum)
|
||||
{
|
||||
printf("Caught signal %d\n",signum);
|
||||
// Cleanup and close up stuff here
|
||||
|
||||
// Terminate program
|
||||
exit(signum);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// Register signal and signal handler
|
||||
signal(SIGINT, signal_callback_handler);
|
||||
|
||||
while(1)
|
||||
{
|
||||
printf("Program processing stuff here.\n");
|
||||
sleep(1);
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Reference in New Issue
Block a user