0% found this document useful (0 votes)
46 views3 pages

Os Prac9

The document describes a lab practical submission containing two C programs, one to implement a system call using fork() and the other to implement the grep command by searching for a pattern in a file.

Uploaded by

22bce363
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views3 pages

Os Prac9

The document describes a lab practical submission containing two C programs, one to implement a system call using fork() and the other to implement the grep command by searching for a pattern in a file.

Uploaded by

22bce363
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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