0% found this document useful (0 votes)
72 views5 pages

Implementation of System Calls

The document discusses the implementation of various system calls in C programming. It includes examples of using exec, exit, wait to execute another process; stat to retrieve file attributes; opendir, closedir, readdir to read a directory; grep to search for a regular expression in a file; and a simulation of the cat command to print the contents of files. Overall the document presents code snippets to demonstrate how common system calls like process control, file manipulation and directory reading are implemented in Linux.

Uploaded by

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

Implementation of System Calls

The document discusses the implementation of various system calls in C programming. It includes examples of using exec, exit, wait to execute another process; stat to retrieve file attributes; opendir, closedir, readdir to read a directory; grep to search for a regular expression in a file; and a simulation of the cat command to print the contents of files. Overall the document presents code snippets to demonstrate how common system calls like process control, file manipulation and directory reading are implemented in Linux.

Uploaded by

Vasantha Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

IMPLEMENTATION OF SYSTEM CALLS

1.exec, exit,wait-command

#include<stdio.h>

#include<sys/types.h>

#include<unistd.h>

#include<stdlib.h>

void main(int argc, char *argv [])

int pid;

/* fork another process */

pid = fork();

if (pid < 0) { /* error occurred */

fprintf (stderr , "Fork Failed") ;

exit(1) ;

else if (pid == 0) { /* child process */

execlp("/bin/ls" , "ls" ,NULL) ;

else { /* parent process */

/* parent will wait for the child to complete */

wait (NULL) ;

printf ("Child Complete") ;

exit (0) ;
}

2)stat command

#include<stdio.h>

#include<sys/stat.h>

main()

int fd;

char pathl[10];

struct stat*nfile;

nfile=(struct stat*)malloc(sizeof(struct stat));

printf("\n Enter the file name:\t");

scanf("%s",pathl);

stat(pathl,nfile);

printf("Program inodeno:%d\n",nfile->st_ino);

printf("Program Size :%d\n",nfile->st_blksize);

printf("Access Time :%s\n",ctime(&nfile->st_atime));

printf("Modified Time :%s\n",ctime(&nfile->st_mtime));

printf("Protection :%d\n",nfile->st_mode);

printf("User id :%d\n",nfile->st_uid);

printf("Group id :%d\n",nfile->st_gid);

printf("Device driverno:%d\n",nfile->st_dev);

printf("No. of links :%d\n",nfile->st_nlink);

return 0;
}

3)opendir ,closedir ,readdir command

#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
main(int argc, char *argv[])
{
struct dirent *dptr;
DIR *dname;
if (argc != 2)
{
printf("Usage: ./a.out <dirname>\n");
exit(-1);
}
if((dname = opendir(argv[1])) == NULL)
{
perror(argv[1]);
exit(-1);
}
while(dptr=readdir(dname))
printf("%s\n", dptr->d_name);
closedir(dname);
}

4.Grep command ((globally search a regular expression and print))

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 1024
void usage()
{
printf("usage:\t. /a.out filename word\n ");
}
int main(int argc, char *argv[])
{
FILE *fp;
char fline[max];
char *newline;
int count=0;
int occurrences=0;
if(argc!=3)
{
usage();
exit(1);
}
if(!(fp=fopen(argv[1],"r")))
{
printf("grep: couldnot open file : %s\n",argv[1]);
exit(1);
}
while(fgets(fline,max,fp)!=NULL)
{
count++;
if(newline=strchr(fline, '\n'))
*newline='\0';
if(strstr(fline,argv[2])!=NULL)
{
printf("%s %d %s\n", argv[1],count, fline);
occurrences++;
}
}
}

[exam41@telnet PROGRAMS]$ cc EX3b.c


[exam41@telnet PROGRAMS]$ ./a.out EX3b.c if
EX3b.c: 16 if(argc!=3)
EX3b.c: 21 if(!(fp=fopen(argv[1],"r")))
EX3b.c: 29 if(newline=strchr(fline, '\n'))
EX3b.c: 31 if(strstr(fline,argv[2])!=NULL)

5.SIMULATION OF CAT
UNIX COMMANDS

#include<stdio.h>
#include<stdlib.h>
void printfile(FILE *f)
{
char str[80];
while(fgets(str,80,f))
printf("%s",str);
}
int main(int argc,char *argv[])
{
char str[80];
FILE *f;
if(argc<2)
printfile(stdin);
else
{
int i;
for(i=1;i<argc;i++)
{
if(!(f=fopen(argv[i],"rt")))
perror(argv[i]);
else
printfile(f);
fclose(f);
}
}
}

ccetstudent@bcse17:~$ gcc ccat.c


ccetstudent@bcse17:~$ ./a.out
welcome
welcome
hi
hi

You might also like