Vous êtes sur la page 1sur 32

http://www.cs.cmu.

edu/~213/oldexams/ MCQ
http://www.cs.toronto.edu/~reid/csc209/tests/
1) Write a Linux shell script (/bin/bash) which has two command line parameters, a username and a file
name. The script should determine the number of times the user with that username is logged into the
system and write that number to the gien file. !f the file already exists, the script should output an error
message. "ome commands you might li#e to use are $grep%, $wc% and $who%.
"olution&
'(/bin/sh
if ) *' +e, - . / then
username0*1
filename0*-
if ) +f *filename . / then
echo 1*filename already exists2
else
set 3who 4 grep 56*username 5 4 wc3
echo *1 7*filename
fi
else
echo 18sage& *9 :username7 :filename72
fi
-) ;ie an example of a Linux file management system call and describe what it does.
Solution: Example: read(fd, buffer, nbytes)
It reads a certain number of data bytes from a file to a buffer and returns the number of bytes read or an
error code
<) Write a = program which uses Linux system calls to create a new process. The child process should
create an empty file called $abc% and then terminate. The parent process should wait for the child process
to terminate and then output the process number of the child process. >on?t forget to chec# for error
conditions.
'include :stdio.h7
'include :sys/types.h7
main()
@
pidAt child/
int status/
B!LC Dfp/
child0for#()/
if (child00+1)
printf(5Crror creating child process5)/
else
if (child009) @
fp 0 fopen(5abc5, 5w5)/
if (fp00E8LL) printf(5Crror creating file5)/
else
fclose(fp)/
F
else @
child0wait(Gstatus)/
if (child00+1) printf(5Crror waiting for child process5)/
else printf(5HldIn5, (long)child)/
F
}
J) Write an explanatory note on enironment ariables. Klso write a = program that outputs the contents of
its enironment list.
Kns& The enironment list is an array of character pointers, with each pointer containing the address of
a null+terminated = string. The address of the array of pointers is contained in the global ariable
environ&
extern char DDeniron/
We?ll call environ the environment pointer, the array of pointers the enironment list, and the
strings they point to the environment strings.
Ly conention, the enironment consists of
name=value strings,
=/=MM program that outputs the contents of its enironment list.
'include 55
!nt main(int argc, char Darg).)
@
int i/
char DDptr/
extern char DDeniron/
for (ptr 0 eniron/ Dptr (0 9/ ptrMM) /D and all en strings D/
printf(5HsIn5, Dptr)/
exit(9)/
F
N) When a process creates a new process using the for#() operation, which of the following state is shared
between the parent process and the child processO
P) "tac#
Q) Reap
S) "hared memory segments
Knswer& Tnly the shared memory segments are shared between the parent process and the newly for#ed
child process. =opies of the stac# and the heap are made for the newly created process.
U) Row does the distinction between #ernel mode and user mode function as a rudimentary form of
protection (security) systemO
Knswer& The distinction between #ernel mode and user mode proides a rudimentary form of protection in
the following manner. =ertain instructions could be executed only when the =V8 is in #ernel mode. "imilarly,
hardware deices could be accessed only when the program is executing in #ernel mode. =ontrol oer when
interrupts could be enabled or disabled is also possible only when the =V8 is in #ernel mode. =onse,uently,
the =V8 has ery limited capability when executing in user mode, thereby enforcing protection of critical
resources.
19) Which of the following instructions should be priilegedO
11) "et alue of timer.
1-) Wead the cloc#.
1<) =lear memory.
1J) !ssue a trap instruction.
1N) Turn off interrupts.
1P) Xodify entries in deice+status table.
1Q) "witch from user to #ernel mode.
1S) Kccess !/T deice.
Knswer& The following operations need to be priileged& "et alue of timer, clear memory, turn off interrupts,
modify entries in deice+status table, access !/T deice. The rest can be performed in user mode.
1U) What is the purpose of system callsO
Knswer& "ystem calls allow user+leel processes to re,uest serices of the operating system.
-9) What are the fie maYor actiities of an operating system in regard to process managementO
Knswer&
a. The creation and deletion of both user and system processes
b. The suspension and resumption of processes
c. The proision of mechanisms for process synchroniZation
d. The proision of mechanisms for process communication
e. The proision of mechanisms for deadloc# handling
-1) What are the three maYor actiities of an operating system in regard to memory managementO
Knswer&
a. [eep trac# of which parts of memory are currently being used and by whom.
b. >ecide which processes are to be loaded into memory when memory space becomes aailable.
c. Kllocate and de+allocate memory space as needed
--) What are the three maYor actiities of an operating system in regard to secondary+storage managementO
Knswer&
\ Bree+space management.
\ "torage allocation.
\ >is# scheduling
-<) What is the purpose of the command interpreterO Why is it usually separate from the #ernelO
Knswer& !t reads commands from the user or from a file of commands and executes them, usually by turning
them into one or more system calls. !t is usually not part of the #ernel since the command interpreter is
subYect to changes.
-J) What system calls hae to be executed by a command interpreter or shell in order to start a new
processO
Knswer& !n 8nix systems, a for# system call followed by an exec system call need to be performed to
start a new process. The for# call clones the currently executing process, while the exec call oerlays a
new process based on a different executable oer the calling process.
-N) What is the purpose of system programsO
Knswer& "ystem programs can be thought of as bundles of useful system calls. They proide basic
functionality to users so that users do not need to write their own programs to sole common problems.
-P) What is the main adantage of the layered approach to system designO What are the disadantages of
using the layered approachO
Knswer& Ks in all cases of modular design, designing an operating system in a modular way has seeral
adantages. The system is easier to debug and modify because changes affect only limited sections of the
system rather than touching all sections of the operating system. !nformation is #ept only where it is needed
and is accessible only within a defined and restricted area, so any bugs affecting that data must be limited to
a specific module or layer.
-Q) List fie serices proided by an operating system. Cxplain how each proides conenience to the users.
Cxplain also in which cases it would be impossible for user+leel programs to proide these serices.
Knswer&
-S) Vrogram execution. The operating system loads the contents (or sections) of a file into memory and
begins its execution. K user+leel program could not be trusted to properly allocate =V8 time.
-U) !/T operations. >is#s, tapes, serial lines, and other deices must be communicated with at a ery low
leel. The user need only specify the deice and the operation to perform on it, while the system
conerts that re,uest into deice+ or controller+specific commands. 8ser+leel programs cannot be
trusted to access only deices they should hae access to and to access them only when they are
otherwise unused.
<9) Bile+system manipulation. There are many details in file creation, deletion, allocation, and naming that
users should not hae to perform. Lloc#s of dis# space are used by files and must be trac#ed. >eleting a
file re,uires remoing the name file information and freeing the allocated bloc#s. Vrotections must also
be chec#ed to assure proper file access. 8ser programs could neither ensure adherence to protection
methods nor be trusted to allocate only free bloc#s and de+allocate bloc#s on file deletion.
<1) =ommunications. Xessage passing between systems re,uires messages to be turned into pac#ets of
information, sent to the networ# controller, transmitted across a communications medium, and
reassembled by the destination system. Vac#et ordering and data correction must ta#e place. Kgain,
user programs might not coordinate access to the networ# deice, or they might receie pac#ets
destined for other processes.
<-) Crror detection. Crror detection occurs at both the hardware and software leels. Kt the hardware leel,
all data transfers must be inspected to ensure that data hae not been corrupted in transit. Kll data on
media must be chec#ed to be sure they hae not changed since they were written to the media. Kt the
software leel, media must be chec#ed for data consistency/ for instance, whether the number of
allocated and unallocated bloc#s of storage match the total number on the deice. There, errors are
fre,uently process+independent (for instance, the corruption of data on a dis#), so there must be a global
program (the operating system) that handles all types of errors. Klso, by haing errors processed by the
operating system, processes need not contain code to catch and correct all the errors possible on a
system.
<<) What are the main adantages of the micro#ernel approach to system designO
Knswer& Lenefits typically include the following (a) adding a new serice does not re,uire modifying the
#ernel, (b) it is more secure as more operations are done in user mode than in #ernel mode, and (c) a simpler
#ernel design and functionality typically results in a more reliable operating system.
<J) =onsider the two+dimensional array K&
int K).). 0 new int)199.)199./
where K)9.)9. is at location -99, in a paged memory system with pages of siZe -99. K small process is in
page 9 (locations 9 to 1UU) for manipulating the matrix/ thus, eery instruction fetch will be from page 9.
Bor three page frames, how many page faults are generated by the following array+initialiZation loops,
using LW8 replacement, and assuming page frame 1 has the process in it, and the other two are initially
emptyO
<N) for (int Y 0 9/ Y : 199/ YMM)
for (int i 0 9/ i : 199/ iMM)
K)i.)Y. 0 9/
<P) for (int i 0 9/ i : 199/ iMM)
for (int Y 0 9/ Y : 199/ YMM)
K)i.)Y. 0 9/
Knswer&
a. N9
b. N,999
<Q) What is a Linux VrocessO
Knswer&
K process generally includes
a data section, which contains global ariables,
the process stac#, which contains temporary data (such as function parameters, return
addresses, and local ariables),
a heap, which is memory that is dynamically allocated during process run time.
The structure of a process in memory is shown in Big. below&
.
<S) Write a = program that ta#es a command and its arguments as arguments, runs the command piped into
sort, and returns the status of that command. Bor example&
./a.out diff file1 file-
is e,uialent to&
diff file1 file- 4 sort
and returns the return status of diff.
// KE"WCW& Bor# - children, one to run the command (not necessarily diff)
// and the second to sort. Vipe the first to the second. Varent waits
// for both children, identifies the first child by its pid and
// returns its status.
int main(int argc, char DDarg) @
int pid1, fd)-., status/
pipe(fd)/
if ((pid1 0 for#()) GG for#()) @ // parent
close(fd)9.)/
close(fd)1.)/
if (wait(Gstatus) (0 pid1)
wait(Gstatus)/
return status 77 S/ // of first child
F
else if (pid1) @ // first child, pipe command output
close(fd)9.)/
dup-(fd)1., 1)/
close(fd)1.)/
execp(arg)1., argM1)/
F
else @ // second child, sort from pipe
close(fd)1.)/
dup-(fd)9., 9)/
close(fd)9.)/
execlp(5sort5, 5sort5, 9)/
F
F
<U) Write 8E!] commands to &
J9) Xa#e all files in your home directory readable only by you and not writable or executable by anyone.
J1) >isplay the path.
J-) =ompile fool.c in the bac#ground with standard output and standard error discarded.
J<) Kdd /usr/Yassi to your path.
JJ) Write a = program to print 5hello world5 and a linefeed to standard output using the write() system call.
Knswer&
chmod J99 ^/D
echo *VKTR
gcc fool.c 7 /de/null -7G1 G (in LK"R)
set VKTR 0 *VKTR&/usr/Yassi
oid main(oid) @write(1,5hello worldIn5,1-)/F
JN) =ircle true or false.
ANSWERS
T The processor must be in kernel mode to set segment registers.
T A disk drive may interrupt a user process before its time slice is up.
T A segmentation fault will put the processor in kernel mode.
!rocesses owned by root always run in kernel mode.
T The boot block contains code which is run when the computer is turned on
A "ombie process is removed when it receives a S#$%&'( signal.
T %alling sleep)* may raise a process+s priority.
T The ,ob of the --. is to translate logical addresses to physical.
The ,ob of the page daemon is to load pages into memory.
T fork)* sets the copy/on/write flag of a processor+s pages.
JP) Write a = program to run 5cmp file1 file- 7 /de/null5 using Linux system calls and then print the exit
status of cmp to the screen. !f status 0 9 then files are same otherwise they are different.
// KE"WCW
'include :stdio.h7
'include :fcntl.h7
'include :unistd.h7
'include :sys/wait.h7
main() @
int fd, status/
if (for#()) @ // parent
wait(Gstatus)/
printf(5status 0 HdIn5, status77S)/
F
else @ // child
fd0open(5/de/null5, TAWWTEL_)/
dup-(fd, 1)/
execlp(5cmp5, 5cmp5, 5file15, 5file-5, E8LL)/
F
F
JQ) What - things does a system call usually do in case of an errorO
KE"WCW& set errno and return +1
JS) Row many processes are created when the following program is runO
main() @
for#()/ for#()/ for#()/
F
KE"WCW& S
JU) What is a ZombieO List - eents that will #ill a Zombie.
KE"WCW& K Zombie is a process that has exited but its parent has not accepted its exit status so its
process !> is still allocated.
The following are the eents that will #ill a Zombie& the parent exits, the parent executes wait(), or the
parent handles the "!;=RL> signal (such as signal("!;=RL>, "!;A!;E)).
N9) Write a = statement that will suspend process 1-<J.
KE"WCW& #ill(1-<J, "!;"TTV)/ or #ill(1-<J, "!;"TV)/
N1) Write a = function runpipe() that ta#es two commands as arrays of strings (li#e arg), executes them
both in parallel with the output of the first piped to the input of the second, waits until both commands
hae finished, and returns the exit status of the second command, e.g.
charD cmd1). 0 @5ls5, 5+las5, 5+W5, E8LLF/
charD cmd-). 0 @5sort5, 5+r5, E8LLF/
int status 0 runpipe(cmd1, cmd-)/ // e,uialent to& ls +las +W 4 sort +r
printf(5done, cmd- returns status HdIn5, status)/
// KE"WCW. This solution creates - children, one for each command,
// then waits for both. "ince they can exit in either order, it
// identifies the second command by its process !>.
int runpipe(charDDarg1, charDDarg-) @
int fd)-./
int pid1, pid-, pid/
int status1, status-/
pipe(fd)/
// Vipe output of cmd1 in a child process
if ((pid10for#())009)@
close(fd)9.)/
dup-(fd)1., 1)/
close(fd)1.)/
execp(arg1)9., arg1)/
perror(arg1)9.)/
F
// Vipe input of cmd- in another child process
else if ((pid-0for#())009) @
close(fd)1.)/
dup-(fd)9., 9)/
close(fd)9.)/
execp(arg-)9., arg-)/
perror(arg-)9.)/
F
// Wait for both children in the parent
else @
close(fd)9.)/
close(fd)1.)/
wait(Gstatus1)/
pid0wait(Gstatus-)/
if (pid00pid-)
return status-77S/ // cmd- finished last
else
return status177S/ // cmd- finished first
F
F
// K simpler approach that doesn?t re,uire chec#ing the pid is to
// for# a child for cmd- and a grandchild for cmd1.
N-) "uppose a dis# bloc# is -[ bytes (instead of J[) and a pointer is J bytes. Row many double indirect
pointers would an inode need to support file siZes up to J ;LO
KE"WCW& S (K bloc# holds N1- pointers. S x N1- x N1- x -[ 0 J ;L)
N<) What is the difference between a sleeping process and a suspended processO
KE"WCW& "leeping 0 waiting on a system call, such as read(). "uspended 0 stopped (e.g. 6`) and waiting
for a "!;=TET signal.
NJ) Write a bash script 5lin#5 that ta#es - file names as arguments and creates a symbolic lin# from the first
to the second, e.g.
lin# foo bar
ls +las foo bar
lrwxrwxrwx 1 user group 1-< "ep -S 1J99 foo +7 bar
+rw+rw+rw+ 1 user group 1-< "ep -S 1J99 bar
KE"WCW&
'(/bin/bash
ln +s *- *1
NN) Write a program in = that copies standard input to standard error using 8E!] system calls for !/T and a
bloc# siZe of J9UP.
/D KE"WCW (with no error chec#ing or header files) D/
main() @
char buf)J9UP./
while (1) @
int n 0 read(9, buf, J9UP)/ /D n 0 number of bytes read D/
if (n :0 9) /D CTB or errorO D/
brea#/
write(-, buf, n)/
F
F
NP) "uppose that fd is a file descriptor. Row do you find the position of the file pointer without moing it.
KE"WCW& lsee#(fd, 9, "CC[A=8W)/
NQ) Write = code to set the permissions of file 5fool5 so that only the owner may read or write it
KE"WCW& chmod(5fool5, 9P99)/
NS) Row many processes exist after the followingO
if (for#()) for#()/
KE"WCW& <
NU) What does this printO
int s0-, p0for#()/
if (p) @
p +0 wait(Gs)/
printf(5s0Hd p0HdIn5, s, p)/
F
else
exit(s)/
KE"WCW& s0N1- p09
(for#() and wait() both return the child pid, so their difference is always 9. exit(-) in the child sets the
high S bits of s in the parent& - :: S is N1-).
P9) Write a = or =MM function run(cmd, file) that executes cmd with the output redirected to file. !f the
command cmd exists, then the function should return its exit status, otherwise it should print an error
message (command cmd not found) and return 9, for example&
int status 0 run(5ls5, 5foo5)) // e,uialent to ls 7 foo
printf(5ls exited with status HdIn5, status)/
status 0 run(5blah5, 5foo5)/ // prints 5command blah not found5, status09
// KE"WCW
int run(char Dcmd, char Dfile) @
if (for#()) @ // parent
int status/
wait(Gstatus)/
return status 77 S/
F
else @ // child
int fd 0 open(file, TAWWTEL_ 4 TA=WCKT 4 TATW8E=, 9P99)/
// may want to chec# for errors here
dup-(fd, 1)/
close(fd)/ // optinal, but a good idea
execlp(cmd, cmd, 9)/ // parent returns cmd?s exit status
fprintf(stderr, 5command Hs not foundIn5, cmd)/ // remember stdout is redirected
exit(9)/ // parent returns 9
F
F
P1) What are the < memory segments allocated to each processO
KE"WCW& code, data, stac#.
P-) What is the difference between a process and a threadO
KE"WCW& threads share memory (code and data), processes do not.
P<) List the J states that a running 8E!] process may transition to. Bor each state, what eent will cause it to
transition to that stateO
KE"WCW
ready or runnable + timeout
suspended + "!;"TTV signal
sleeping + wait for eent (such as !/T)
Zombie + exit or #illed
PJ) What information is stored in an inodeO
KE"WCW& pointers to dis# bloc#s, plus all information returned by stat() (i.e. eerything except the
filename)& file type, real and effectie owner and group !>, permissions, hard lin# count, last
accessed/modified/changed time.
PN) Write a = program that illustrates the creation of child process using for# system call. Tne process finds
sum of een series and other process finds sum of odd series.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
int main()
{
int i,n,sum=0;
pid_t pid;
system(clea!);
pintf("nte n #alue$!);
scanf(%d!,&n);
pid=fo'();
if(pid==0)
{
pintf((om child pocess)n!);
fo(i=*;i<n;i+=,)
{
pintf(%d)!,i);
sum+=i;
-
pintf(.dd sum$%d)n!,sum);
-
else
{
pintf((om pocess)n!);
fo(i=0;i<n;i+=,)
{
pintf(%d)!,i);
sum+=i;
-
pintf("#en sum$%d)n!,sum);
-
-
PP)
PQ)
PS)
Write a simple C proram in which a parent process sends the strin !" lo#e
PU) Write a single user program for T"/1P1 that will generate one child process, which in turn will generate a
grandchild process (< processes in total). The output should be the sum of the three pids. Bor simplicity,
assume that pidAt is an int.
$he %ollowin %unctions miht &e help%ul:
int printf(const char *format, ...);
pid_t getpid(void);
pid_t fork(void);
pid_t waitpid(pid_t pid, int *status, int options); // opt=0
void _exit(int exitcode);
int main)* 0
int parent1 child1 grandchild2
parent 3 )int* getpid)*2
child 3 )int* fork)*2
if )child 33 4* 0
grandchild 3 )int* fork)*2
5e6it)grandchild*2
7 else 0
waitpid)child18grandchild14*2
printf)9:d;n91 parent < child < grandchild*2
7
7
/********************
another fun exampe!
********************/
int main() "
int sum = (int) getpid();
if (fork() == 0) "
sum #= (int) getpid();
if (fork() == 0) "
sum #= (int) getpid();
printf($%d&n$,sum);
'
'
F
Q9) Row many processes does the following piece of code createO WhyO
int main()
"
fork();
fork();
return 0;
'
The code creates four processes. hen executed, a process is created. The first
for!() creates a child process. Then both the parent and the child "ill execute the
last for!# each creates a process. Therefore, a total of four processes "ill be created.
Q1)
/* (i)onacci *eries (+r , - singa) fi)o.c* /
/* .ncudes */
/incude 0unistd.h1 /* *2m)oic 3onstants */
/incude 0s2s/t2pes.h1 /* 4rimitive *2stem +ata 52pes */
/incude 0errno.h1 /* 6rrors */
/incude 0stdio.h1 /* .nput/7utput */
/incude 0s2s/wait.h1 /* 8ait for 4rocess 5ermination */
/incude 0stdi).h1 /* 9enera :tiities */

int main(int argc, char *argv;<)
"
int a=0, )==,i,ii,n;
pid_t pid;
// if(argc 0 >)
// "
// printf($*2nposis! fi)o= 0/ of terms of fi)onacci series1&n$);
// return 0;
// '
printf($6nter the num)er of a (i)onacci *e?uence!&n$);
scanf($%d$, @ii);
// ii = atoi(argv;=<);
if (ii 0 0)"
printf($4ease enter a nonAnegative integerB&n$);exit(=);'
ese
"
pid = fork();
if (pid == 0)
"
printf($3hid is producing the (i)onacci *e?uence...&n$);
printf($%d %d $,a,));
for (i=>;i0ii;i##)
"
n=a#);
printf($%d $, n);
a=);
)=n;
'
printf($&n3hid ends&n$);
'
ese
"
printf($4arent is waiting for chid to compete...&n$);
wait(C:DD);
printf($4arent ends&n$);
'
'
return 0;
'
/* (i)onacci *eries using command ine arguments (+r , - singa) fi)o=.c*/
/* .ncudes */
/incude 0unistd.h1 /* *2m)oic 3onstants */
/incude 0s2s/t2pes.h1 /* 4rimitive *2stem +ata 52pes */
/incude 0errno.h1 /* 6rrors */
/incude 0stdio.h1 /* .nput/7utput */
/incude 0s2s/wait.h1 /* 8ait for 4rocess 5ermination */
/incude 0stdi).h1 /* 9enera :tiities */

int main(int argc, char *argv;<)
"
int a=0, )==,i,ii,n;
pid_t pid;
if(argc 0 >)
"
printf($*2nposis! fi)o= 0/ of terms of fi)onacci series1&n$);
return 0;
'
// printf($6nter the num)er of a (i)onacci *e?uence!&n$);
// scanf($%d$, @ii);
ii = atoi(argv;=<);
if (ii 0 0)"
printf($4ease enter a nonAnegative integerB&n$);exit(=);'
ese
"
pid = fork();
if (pid == 0)
"
printf($3hid is producing the (i)onacci *e?uence...&n$);
printf($%d %d $,a,));
for (i=>;i0ii;i##)
"
n=a#);
printf($%d $, n);
a=);
)=n;
'
printf($&n3hid ends&n$);
'
ese
"
printf($4arent is waiting for chid to compete...&n$);
wait(C:DD);
printf($4arent ends&n$);
'
'
return 0;
'
$ cat talk.c ...list the program.
#include <stdio.h>
#define READ 0 /* he inde! of the read end of the pipe */
#define "R#E $ /* he inde! of the %rite end of the pipe */
char* phrase & '(tuff this in )our pipe and smoke it'*
main +,
-
int fd ./01 2)tesRead*
char message .$000* /* 3arent process4 message 2uffer */
pipe +fd,* /*5reate an unnamed pipe */
if +fork +, && 0, /* 5hild1 %riter */
-
close+fd.READ0,* /* 5lose unused end */
%rite +fd."R#E01phrase1 strlen +phrase, 6 $,* /* include 7899*/
close +fd."R#E0,* /* 5lose used end*/
:
else /* 3arent1 reader*/
-
close +fd."R#E0,* /* 5lose unused end */
2)tesRead & read +fd.READ01 message1 $00,*
printf +'Read ;d 2)tes< ;s=n'1 2)tesRead1 message,* /* (end */
close +fd.READ0,* /* 5lose used end */
:
:
$ ./talk ...run the program.
Read >? 2)tes< (tuff this in )our pipe and smoke it
$ @
Exercise:
5he :C.E command ine
who F sort
uses a pipe. Dook at the foowing fragment of a 3 program, intended to
have the e?uivaent functionait2.
5he parent process executes who, whie the chid executes sort. .n the space
provided )eow, write the 3 code for $*ection G$ and $*ection H$ to aow the
chid and parent to communicate proper2.
/incude 0stdio.h1
int main()
" int pid, fd;><;
pipe( fd );
if( (pid=fork()) == 0 )
" /* *ection G! pace appropriate code here! */
execp( $sort$, $sort$, (char *) 0 );
'
ese
" /* *ection H! pace appropriate code here! */
execp( $who$, $who$, (char *) 0 );
'
'
(olution to the e!ercise
$ cat connect.c ...list the program.
#include <stdio.h>
#define READ 0
#define "R#E $
main +argc1 argA,
int argc*
char* argA .0*
-
int fd ./0*
pipe +fd,* /* 5reate an unnamed pipe */
if +fork +, B& 0, /* 3arent1 %riter */
-
close +fd.READ0,* /* 5lose unused end */
dup/ +fd."R#E01 $,* /* Duplicate used end to stdout */
close +fd."R#E0,* /* 5lose original used end */
e!eclp +argA.$01 argA.$01 7899,* /* E!ecute %riter program */
perror +'connect',* /* (hould neAer e!ecute */
:
else /* 5hild1 reader */
-
close +fd."R#E0,* /* 5lose unused end */
dup/ +fd.READ01 0,* /* Duplicate used end to stdin */
close +fd.READ0,* /* 5lose original used end */
e!eclp +argA./01 argA./01 7899,* /* E!ecute reader program */
perror +'connect',* /* (hould neAer e!ecute */
:
:
$ %ho ...e!ecute '%ho' 2) itself.
glass pts/$ Ce2 $D $E<FD +<0.0,
$ ./connect %ho %c ...pipe '%ho' through '%c'.
$ G F/ ...$ line1 G %ords1 F/ chars.
$ @
Q-)
"olution&
#!/bin/sh
size=$1
for file in *
do
set `du -b $file`
if [ $1 -gt $size ]
then
echo $file $1
fi
done
Q<)
/incude 0stdio.h1
/incude 0s2s/stat.h1
/incude 0unistd.h1
/incude 0dirent.h1
int main(void)
"
+., *dp;
struct stat )uf;
struct dirent *entr2;
dp = opendir($.$);
for(entr2 = readdir(dp); entr2 B= C:DD; entr2 = readdir(dp)) "
stat(entr2A1d_name, @)uf);
if(*_.*+.,()uf.st_mode))"
continue;
'
if()uf.st_siIe 1 =0>J00) "
printf($%s %d&n$, entr2A1d_name, )uf.st_siIe);
'
'
'
/D Row much does !/T oerhead matterO
D Let?s see how performance changes when we ma#e small !/T calls,
D compared with large ones.
D
D 8sage& ./copy infile outfile bloc#AsiZe
D
D Klso, there?s one bad coding decision here that might cause
D us some trouble unrelated to !/T .... can you spot itO
D/
'include :stdio.h7
'include :stdlib.h7
'include :assert.h7
'include :fcntl.h7
'include :errno.h7
int main(int argc, charDD arg) @
assert(argc 00 J)/
charD inApath 0 arg)1./
charD outApath 0 arg)-./
siZeAt bufsiZe 0 atoi(arg)<.)/

int inAfd 0 open( inApath, TAW>TEL_)/
int outAfd 0 open(outApath, TAWWTEL_ 4 TA=WCKT, "A!WW]8)/
assert( inAfd 7 9)/
assert(outAfd 7 9)/

int done 0 9/
char buf)bufsiZe./
while (( done) @
int bytesAread 0 read(inAfd, buf, bufsiZe)/
if (bytesAread : 9 GG errno 00 CK;K!E)
/ /D Yust try again D/
else if (bytesAread :0 9)
done 0 1/
else @
int bytesAwritten 0 9/
while (bytesAwritten : bytesAread) @
int writtenAthisAtime 0 write(outAfd, G buf)bytesAwritten.,
bytesAread + bytesAwritten)/
if (writtenAthisAtime : 9 GG errno 00 CK;K!E)
/
else if (writtenAthisAtime :0 9)
assert(9)/
else
bytesAwritten M0 writtenAthisAtime/
F
F
F

close( inAfd)/
close(outAfd)/
F
"olution&
#!/bin/sh
size=$1
for file in *
do
set `du -b $file`
if [ $1 -gt $size ]
then
echo $file $1
fi
done
QJ)
QN) K
QP) K
Kill() and signal() (Dr R K Singla)
Let us now write a program that communicates between child and parent
processes using kill() and signal().
fork() creates the child process from the parent. The pid can be checked to decide
whether it is the child(==0) or the parent (pid = child process id).
The parent can then send messages to child using the pid and kill().
The child picks up these signals with signal() and calls appropriate functions.
/* An eample of communicating process using signals is sig!talk.c" */
/* sig!talk.c ### $ample of how % processes can talk to each other using kill() &
signal() */
/* 'e will fork() % process and let the parent send a few */
/* signals to it(s child */
/* gcc sig!talk.c #o sig!talk */
)include *stdio.h+
)include *signal.h+
,oid sighup()- /* routines child will call upon sigtrap */
,oid sigint()-
,oid sig.uit()-
main()
/ int pid-
/* get child process */
if ((pid = fork()) * 0) /
perror(0fork0)-
eit(1)-
2
if (pid == 0)
/ /* child */
signal(3456789sighup)- /* set function calls */
signal(3454:T9sigint)-
signal(345;74T9 sig.uit)-
for(--)- /* loop for e,er */
2
else /* parent */
/ /* pid hold id of child */
printf(0<n8A=$:T" sending 345678<n<n0)-
kill(pid9345678)-
sleep(>)- /* pause for > secs */
printf(0<n8A=$:T" sending 3454:T<n<n0)-
kill(pid93454:T)-
sleep(>)- /* pause for > secs */
printf(0<n8A=$:T" sending 345;74T<n<n0)-
kill(pid9345;74T)-
sleep(>)-
2
2
,oid sighup()
/ signal(3456789sighup)- /* reset signal */
printf(0?64L@" 4 ha,e recei,ed a 345678<n0)-
2
,oid sigint()
/ signal(3454:T9sigint)- /* reset signal */
printf(0?64L@" 4 ha,e recei,ed a 3454:T<n0)-
2
,oid sig.uit()
/ printf(0AB @A@@C has Dilled meEEE<n0)-
eit(0)-
2
QQ)
$ipes
' pipe is an interprocess communication (")C* mechanism %or two +nix process
runnin on the same machine. ' pipe is a one,wa- communication channel &etween
the two processes (one process alwa-s writes to one end o% the pipe. the other
process alwa-s reads %rom the other end o% the pipe*. ' pipe is li/e a temporar- 0le
with two open ends. writes to one end &- one process can &e read %rom the other
end &- the another process. 1owe#er. unli/e a reular 0le. a read %rom a pipe
results in the remo#al o% data that was written &- the other process. $he pipe
s-stem call is used to create a new pipe. "t creates two 0le descriptors. one %or the
read end o% the pipe. the other %or the write end o% the pipe. 2ne o% the
communicatin processes use the write end o% the pipe to send a messae to the
other communication process who reads %rom the read end:
When your shell program executes a command with a single pipe li#e the following&
cat foo.txt F grep Ai )ah
cat3s output will &e pipe3ed to grep3s input. $he shell process will %or/ two process
(one that will exec cat the other that will exec grep*. and will wait %or &oth to 0nish
&e%ore printin the next shell prompt. +se pipe and "/2 redirection to set up
communication &etween the two child processes.
The pipe reading process #nows when to exit when it reads CTB on its input/ any process bloc#ed on a read
will unbloc# when the file is closed. Roweer, if multiple processes hae the same file open, only the last
close to the file will trigger an CTB. Therefore, when you write programs that create pipes (or open files), you
need to be ery careful about how many processes hae the files open, so that CTB conditions will be
triggered.
QS)

Vous aimerez peut-être aussi