You are on page 1of 3

Lab Practical Submission

Date: 12/04/24
Roll No. and Name: 22BCE305 ROSHNI PANKAJKUMAR RANA
Course Code and Name: 2CS201 OPERATING SYSTEM
Practical No.: 9

a) Write a C program to implement a system call using the fork()

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main()
{
int pid;
pid=fork();
if ( pid==0 ){
printf("child created\n");
printf("the ppid are : %d %d", getppid(), getppid());
}
else{
if (pid=0) {printf("failed\n");}
else{
printf("Parent is waiting\n");
wait(NULL);
printf("child terminated\n");
}
}
}
b) Write a C program to implement grep command.

//fp -> location of the file, file pointer


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

void main(){
FILE *fp;
char patt[100], temp[100];
int flag=0;
fp=fopen("fruit.txt", "r"); //opened in read mode (if
the file is in the same folder as we are in)
printf("Enter pattern:");
scanf("%s",patt);
while(!feof(fp)){ // feop - file end of file

fgets(temp,100,fp); //temp - buffer location , max


size 1000

if(strstr(temp, patt)){ //strstr to find substring


-> true if patt present in temp
flag=1;
break;
}

else {
flag=0;
}
}
if(flag==1){
printf("Pattern found\n");
}
else{
printf("Pattern not found\n");
}
fclose(fp);

You might also like