0% found this document useful (0 votes)
167 views

Linux

The document contains several code examples demonstrating the use of various system calls in C programs. Example 1a shows the fork system call to create child processes. Example 1b demonstrates directory reading using opendir, readdir, and closedir. Example 1c shows the use of exec to execute a new program. The remaining examples show opening, reading, and writing files using the open, read, and write system calls. Examples 3a and 3b simulate the ls and grep Unix commands.

Uploaded by

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

Linux

The document contains several code examples demonstrating the use of various system calls in C programs. Example 1a shows the fork system call to create child processes. Example 1b demonstrates directory reading using opendir, readdir, and closedir. Example 1c shows the use of exec to execute a new program. The remaining examples show opening, reading, and writing files using the open, read, and write system calls. Examples 3a and 3b simulate the ls and grep Unix commands.

Uploaded by

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

Ex.

no:1a PROGRAM FOR SYSTEM CALLS (Fork, Getpid, Exit)


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void main()
{
int pid,pid1,pid2;
pid=fork();
if(pid==-1)
{
printf("ERROR IN PROCESS CREATION \n");
exit(1);
}
if(pid!=0)
{
pid1=getpid();
printf("\n the parent process ID is %d\n", pid1);
}
else
{
pid2=getpid();
printf("\n the child process ID is %d\n", pid2);
}
}
Ex.no:1b PROGRAM FOR SYSTEM CALLS (OPENDIR, READDIR, CLOSEDIR)
#include<stdio.h>
#include<dirent.h>
#include <stdlib.h>
struct dirent *dptr;
void main(int argc, char *argv[])
{
char buff[100];
DIR *dirp;
printf(“\n\n ENTER DIRECTORY NAME”);
scanf(“%s”, buff);
if((dirp=opendir(buff))==NULL)
{
printf(“The given directory does not exist”);
exit(1);
}
while(dptr=readdir(dirp))
{
printf(“%s\n”,dptr->d_name);
}
closedir(dirp);
}
Ex.no:1c PROGRAM FOR SYSTEM CALLS(EXEC)

#include <stdio.h>
#include <unistd.h>
using namespace std; Output:
9755
int main(int argc, char *argv[]) { 9755
int pid ; Not a problem!
char *args[2] ; 00OOOpppssss.
pid = fork() ;
printf("%d\n",pid);
if (pid ==0)
{ args[0] = "./a.out" ;
args[1] = NULL ;
execv("./a.out", args) ;
printf("%d\n",pid);
printf("OOOpppssss.\n") ;
}
else
{printf("%d",pid);
printf("Not a problem!\n") ;}
return 0;
}
Ex.no:2a OPEN SYSTEM CALL
PROGRAM:
#include<stdio.h>
int main()
{
int fd;
if((fd=open(“file.dat”))==-1)
{
perror(“cannot open the file.dat”);
exit(0);
}
else
printf(“\n FILE OPENED SUCCESSSFULLY”);
return 0;
}
OUTPUT:
FILE OPENED SUCCESSSFULLY
Ex.no:2b READ SYSTEM CALL
#include<stdio.h>
main()
{
char b[20];
int fd,xr;
if((fd=open(“write”,0))==-1)
{
printf(“cannot open file”);
exit(1);
}
do
{
xr=read(fd,b,20);
b[xr]=’\0’;
printf(“%s”,b);
}
while(xr==20);
close(fd);
}
OUTPUT:
balaji.S
II YEAR
Ex.no: 2c WRITE SYSTEM CALL

#include<stdio.h>
main(int ac,char*av[])
{int fd;
int i=1;
char*sep=” “;
if(ac<1)
{printf(“\n INSUFFICIENT ARGUMENTS");
exit(1);}
if((fd=open(“balaji”,0660))==-1)
{printf(“\n CANNOT CREATE THE FILE”);
exit(1);}
while(i<ac)
{write(fd,av[i],(unsigned)strlen(av[i]));
write(fd,sep,(unsigned)strlen(sep));
i++;}
close(fd);}
OUTPUT:
cc write.c
./a.out balaji
cat balaji
os lab
balaji
Ex.no: 3a PROGRAM FOR SIMULATION OF LS UNIX
#include<stdio.h>
COMMANDS
#include<dirent.h>
main(int argc, char **argv)
{
DIR *dp;
struct dirent *link;
dp=opendir(argv[1]);
printf(“\n contents of the directory %s are \n”, argv[1]);
while((link=readdir(dp))!=0)
printf(“%s”,link->d_name);
closedir(dp);
}
SAMPLE OUTPUT:
Cc list.c
./a.out os
CONTENTS OF THE DIRECTORY OS ARE
Priority.c
Robin.c
copy
Ex.no:3b PROGRAM FOR SIMULATION OF GREP UNIX
COMMANDS
#include<stdio.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++;}}}

SAMPLE OUTPUT

CAT>SAMP

ONE

ONE TWO

THREE FOUR

Cc grep.c

./a.out samp one

Samp:1 one

Samp:2 one two

You might also like