Sale!

CECS-326: Operating Systems Lab 4 Using fork() and exec()

$30.00

Category:
5/5 - (2 votes)

CECS-326: Operating Systems
Lab 4
Using fork() and exec()
The parent generates a child process and then overlays it by a call to exec.
/*
PROGRAM: lab4.c
*/
# include <stdio.h>
# include <unistd.h>
void main()
{
static char *mesg[] = {“0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”};
int display(char *), i;
for (i=0; i<10; ++i)
display(mesg[i]);
sleep(2);
}
int display(char *m)
{
char err_msg[25];
switch (fork())
{
case 0:
execlp(“/bin/echo”, “echo”, m, (char *) NULL);
sprintf (err_msg, “%s Exec failure”, m);
perror(err_msg); return(1);
case -1:
perror (“Fork failure”); return(2);
default:
return(0);
}
}
a) Confirm that there is /bin/echo. Change if necessary. Check for program error, compile to an excutable
file named lab4a, and run. Observe the results. Observe if the order of the messages changes when run
successive times. Why?
b) Replace “/bin/echo” in execlp() with a non-existing directory, or with an existing directory with nonexisting executable file. Compile to an executable file named lab4b, run, and observe results.
c) Replace return(1) by exit(1) in b) above. Compile to an executable file named lab4c, run, and observe
results.
END OF LAB 4

PlaceholderCECS-326: Operating Systems Lab 4 Using fork() and exec()
$30.00
Open chat
Need help?
Hello
Can we help?