Professional Documents
Culture Documents
bcs_2020014@iiitm.ac.in
Q1:
Code:
#include<iostream>
#include<unistd.h>
int main(){
int pid;
pid = fork();
if (pid == 0){
cout << "\nChild Process with parent id : "<< getppid() << endl;
return 0;
Output:
Q2:
Code:
#include<iostream>
#include<unistd.h>
#include<fcntl.h>
int main(){
int fd;
char buffer[50];
fd = open("oslab.txt",O_RDWR);
printf("fd = %d",fd);
if(fd != -1){
write(fd,msg,sizeof(msg));
lseek(fd,0,SEEK_SET);
read(fd,buffer,sizeof(msg));
close(fd);
return 0;
Output:
Q3:
Code:
#include<iostream>
#include<unistd.h>
int main(){
int pid;
pid = fork();
if (pid == 0){
sleep(10);
cout<<"child\n";
return 0;
Output:
After 10 seconds:
Q4:Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main() {
int pid; //process id
pid = fork();
if (pid<0){
printf("\nFork failed\n");
exit (-1);
}
else if (pid==0){
execlp("/bin/ls","ls","-l",NULL);
}
else {
wait (NULL);
printf("\nchild complete\n");
exit (0);
}
Output:
Q5:
Code:
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
int main(){
if(pid==0){
system("ls");
else{
exit(0);
return 0;
Output: