Exercise 2
ps
command to show the processes
you have in cs-computer. How to use kill
command to delete processes ? Show some examples in exercises.
nice
command to change the scheduling
priority of the process ? Show an example in cs-computer.
Save the programs and your solutions of the following tasks to cs-computer so that you can access the files in exercise session.
PID
and PPID
of these
two processes.
#include <stdio.h> main() { int pid; pid = fork(); if (pid < 0) { fprintf(stderr,"Fork failed"); exit(-1); } if (pid == 0) { execlp("/usr/local/bin/top","top",NULL); } else { wait(NULL); printf("Child complete\n"); exit(0); } }
/bin/finger
and in the second child process,
execute /bin/who
. Use
while(wait(NULL) != -1);
to wait until the both child
processes are being executed. Compile the program in cs
and execute the program.
man pipe
and explain in detailed
what the following program does. Compile the program in
cs and execute the program.
#include <stdio.h> #include <string.h> main() { int putki[2], pid, N; if (pipe(putki) != 0) {perror("pipe"); exit(1);} pid = fork(); if (pid == -1) {perror("fork"); exit(1);} if (pid != 0) { char message1[1024]; close(putki[1]); if (read(putki[0],message1,1024) < 0) {perror("read putki"); exit(1);} printf("Child: %s\n",message1); close(putki[0]); printf("Parent: Yes.\n"); }else{ char message2[1024]; close(putki[0]); strcpy(message2,"Can you hear me ?"); N=strlen(message2); if (write(putki[1],message2,N) != N) {perror("write putki"); exit(2);} close(putki[1]); } exit(0); }