Write a program which creates a child process, waits for the termination of its child and lists its PID, together with the state in which the process was terminated (in decimal and hexadecimal).
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int pid, state;
printf(“before the fork\n”);
if ((pid =fork() != 0))
{
printf(“parent\n”);
wait(&state);
}
else
{
printf(“child\n”);
execl(“./child”, “child”, 0);
perror(“Error exec”);
}
printf(“after fork state = %d\n”,state);
printf(“PID child = %d terminated\n”, pid);
}
#include<sys/types.h>
#include<sys/wait.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
int pid;
printf(“child begins\n”);
pid = getpid();
printf(“child %d ends\n”, pid);
exit(10);
return 1;
}
Write a program which receives from the keyboard the names of some Linux commands without parameters and then executes them. The system call execlp should be used. When waiting for the input of a command, the program should list the prompter ’>’.
#include<sys/types.h>
#include<sys/wait.h>
#include <stdio.h>
#include <unistd.h>
int main(void)
{
char buf[10];
pid_t pid;
int status;
printf(“>”);
while (fgets(buf, 10, stdin) != NULL)
{
buf[strlen(buf)-1] = 0;
if ( (pid = fork()) < 0)
{
perror(“The error fork”);
exit(1);
}
else if (pid == 0)
{
execlp(buf, buf, 0);
perror(“The error exec”);
exit(2);
}
if (( pid = waitpid(pid, &status, 0)) < 0)
{
perror(“the error waitpid”);
exit(2);
}
printf(“> “);
}
exit(0);
}
#include<sys/types.h>
#include<sys/wait.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
int pid, k=7,stat;
pid =fork();
wait(&stat);
printf(” %d returned\n”, pid);
if (pid) k=2;
printf(“k=%d\n”, k);
sleep(5);
printf(“gata bidiu”);
}
Execute the program below on test files with increasing sizes (up to some MB) and explain the result.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int fdR, fdW;
char c;
void rd_wr()
{
for (;;)
{
if (read( fdR, &c, 1) != 1) return;
write( fdW, &c, 1);
}
}
int main(int argc, char * argv[])
{
if (argc != 3) exit(1); //verify if there are enough arguments
if ((fdR=open( argv[1], O_RDONLY)) < 0) { //open the file of the first argument -read
perror(“The error open”);
exit(1);
}
if ((fdW=creat(argv[2], 0600)) < 0) { //create the file of the second argument – write
perror(“The error create”);
exit(1);
}
fork(); //create new process
rd_wr();
exit(0);
}
Write a C program in order to show how the children of a terminated parent process automatically become the children of the init process.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main( void)
{
int pid;
pid= fork();
printf(“the curent processes %d and the parents %d \n”, getpid(), getppid());
if (pid != 0)
exit(0);
else
{
//execl;
sleep(1);
printf(“the curent processes %d and the parents %d \n”, getpid(), getppid());
}
}
Write a C program which creates a process in the zombie state.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main( void)
{
int pid;
pid= fork();
if (pid == 0) //we are in the child
exit(0);
sleep(1);
execlp(“ps”, “ps”, “-la”, NULL);
}
//primul parametru e calea, doar apoi avem ce ne apare in linia de comanda
Write a C program which shows how two processes which are members of the relationship parent-child: are concurrent from the execution’s point of view, initially the child is a copy of the parent, but every process has its own data.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main( void)
{
int pid;
int n=0;
int status;
pid= fork();
while (n<10)
{
if (pid == 0)
{
//we are in the child
printf(“this is the son working: %d\n”, n);
n = n + 2;
sleep(1);
}
else
{
//we are in the parent
// wait(&status);
printf(“this is the parent working: %d\n”, n);
n = n +3;
sleep(1);
}
}
}
Write two C programs: one called client.c, the other called server.c. The client program lists a prompter and reads from the keyboard two integers and one of the characters ’+’ or ’–’. The read information is transmitted with the help of the system call execl to a child process, which executes the server’s code. After the child (server) process finishes the operation, it transmits the result to the parent process (client) with the help of the system call exit. The client process prints the result on the screen and also reprints the prompter, ready for a new reading.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
int pid;
char op1, op2;
char sign;
int status;
printf(“>”);
scanf(“%c %c %c”, &op1, &sign, &op2);
pid = fork();
if (pid == 0)
{
if (execl(“./child.exe”, &op1, &op2, &sign, NULL)<0)
perror(“eroare”);
}
else if (pid>0)
{
waitpid(pid, &status, 0);
printf(“result %d\n”, WEXITSTATUS(status));
}
exit(0);
}
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main( int argc, char **argv)
{
int a;
int b;
int res=0;
a = *argv[0] – 48;
b = *argv[1] – 48;
printf (“\n %d %d “, a, b );
if ( *argv[2] == ‘+’)
res = a+b;
else res = a-b;
return res;
}