You are on page 1of 5

OS LAB Assignment 2

Arun Kumar Rathod

bcs_2020014@iiitm.ac.in

Q1:

Code:

#include<iostream>

#include<unistd.h>

using namespace std;

int main(){

int pid;

pid = fork();

if (pid == 0){

cout << "\nParent Process id : "<< getpid() << endl;

cout << "\nChild Process with parent id : "<< getppid() << endl;

return 0;

Output:
Q2:

Code:

#include<iostream>

#include<unistd.h>

#include<fcntl.h>

using namespace std;

int main(){

int fd;

char buffer[50];

char msg[50]="Hello my name is arun";

fd = open("oslab.txt",O_RDWR);

printf("fd = %d",fd);

if(fd != -1){

printf("\n oslab.txt opened with read write access\n");

write(fd,msg,sizeof(msg));

lseek(fd,0,SEEK_SET);

read(fd,buffer,sizeof(msg));

printf("\n %s was written to file\n",buffer);

close(fd);

return 0;

Output:
Q3:

Code:

#include<iostream>

#include<unistd.h>

using namespace std;

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>

using namespace std;

int main(){

int pid = fork();

if(pid==0){

cout<<"CHILD PROCESS EXECUTING";

system("ls");

else{

cout<<"PARENT PROCESS TERMINATED"<<endl;

exit(0);

return 0;

Output:

You might also like