Vous êtes sur la page 1sur 25

PROGRAM:

/*STAT SYSTEM CALL*/

#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<errno.h>
main()
{
char fname[30];
struct stat buf;
printf("\n enter filename with path");
scanf("%s",fname);
if(stat(fname,&buf)==0)
{
printf("\n filename is %s",fname);
printf("\n the user id of the owner is %d",buf.st_uid);
printf("\n the size of id is %d bytes",buf.st_size);
printf("\n the file permission id %d",buf.st_mode);
}
else
{
perror("stat");
exit(0);
}
}
OUTPUT:

enter filename with path: test

filename is test
the user id of the owner is 604
the size of id is 7587 bytes
the file permission id 33277
PROGRAM:

/* WAIT SYSTEM CALL*/

#include<stdio.h>
main()
{
int pid;
pid=fork();
if(pid<0)
{
printf(stderr,"fork failed");
exit(-1);
}
else if(pid==0)
{
execlp("/bin/ls","ls",NULL);
}
else
{
wait(NULL);
printf("\n Child Complete");
exit(0);
}
}
OUTPUT:

a.out first.c second.c test


c_pgms compile temp

Child Complete
PROGRAM:

/* FORK SYSTEM CALL*/

#include<stdio.h>
#include<sys/types.h>
main()
{
pid_t pid;
pid=fork();
if(pid==0)
{
printf("\n child process starts");
printf("\n the pid of the child process is %d",getpid());
printf("\n child process ends");
}
else if(pid>0)
{
printf("\n parent process atarts");
printf("\n the pid of the parent process is %d",getpid());
printf("\n parent process ends\n");
}
else
{
perror("fork");
}
}
OUTPUT:

child process starts


the pid of the child process is 4524
child process ends
parent process starts
the pid of the parent process is 4523
parent process ends
PROGRAM:

/* I/O SYSTEM CALL FOR CREATE AND CLOSE*/

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
main()
{
char fname[30];
int fd,filemode;
printf("\n enter the file name to be created with the path ");
scanf("%s",fname);
if(fd=creat(fname)<0)
{
perror("create");
}
else
{
printf("\n enter the permission for the file");
scanf("%d",&filemode);
if(chmod(fname,filemode)==0)
{
printf("\nfile is successfully created");
printf("\nfilename\t mode\n");
printf("%s\t\t%d\t\t\n",fname,filemode);
}
}
if((close(fd))<0)
perror("close");
else
printf("\nfile name is closed succesfully ");
}
OUTPUT:

enter the file name to be created with the path: /home/cs4097/vig3

enter the permission for the file 666

file is successfully created


filename mode
/home/cs4097/vig3 666

file is closed succesfully


PROGRAM:

/*I/O SYSTEM CALL FOR READ*/

#include<stdio.h>
#include<sys/file.h>
#include<sys/types.h>
#include<fcntl.h>
#include<limits.h>
main()
{
char fn[30];
int fd,i=0;
int actualbytes, nbytes,pos;
char buffer[1000];
printf("\n enter filename with path");
scanf("%s",fn);
printf("\n opening the file %s \n",fn);
if((fd=open(fn,O_RDWR|O_CREAT|O_RDONLY,0644))<0)
{
perror("open");
exit(0);
}
else
{
printf("\n enter maximum amount of data to be read\n");
scanf("%d",&nbytes);
if(actualbytes=read(fd,buffer,nbytes)<0)
{
perror("read");
exit(0);
}
else
{
printf("\n\n file is opened successfully");
printf("\n reading the first %d bytes from the files \n",nbytes);
printf("\n\n the contents are \n\n %s",buffer);
}
printf("\n\n determining the size of file using lseek");
if((pos=lseek(fd,0,SEEK_END))<0)
{
perror("lseek");
exit(0);
}
else
printf("\n\n the size of the file is %d",pos);

if(close(fd)<0)
{
perror("close");
exit(0);
}
else
printf("\n\n file is closed");
}
}
OUTPUT:

enter filename with path /home/cs4097/myfile.c

opening the file /home/cs4097/myfile.c

enter maximum amount of data to be read


5

file is opened successfully


reading the first 5 bytes from the files

the contents are

#incl

determining the size of file using lseek

the size of the file is 735

file is closed
PROGRAM:

/*I/O SYSTEM CALL FOR WRITE*/

#include<stdio.h>
#include<sys/file.h>
#include<sys/types.h>
#include<limits.h>
main()
{
char fn[30];
int fd,i=0;
int actualbytes,bytesstored;
char buffer[100];
printf("\nenter the filename with path");
scanf("%s",fn);
if((fd=open(fn,O_RDWR|O_CREAT|O_RDONLY,0611))<0)
{
perror("open");
exit(0);
}
else
{
printf("\n\nenter the contents for the file");
printf("press ctrl+d at the end of the file\n\n");
while((buffer[i]=getc(stdin))!=EOF)
i++;
buffer[i]='\0';
bytesstored=100;
if(actualbytes=write(fd,buffer,bytesstored)<0)
{
perror("write");
exit(0);

}
else
{
printf("\n\nfile is opened successfully");
printf("\nthe contents are written");

}
if(close(fd)<0)
{
perror("close");
exit(0);

}
else
{
printf("file closed");
}

}
}
OUTPUT:

enter the filename with path tamil

enter the contents for the file press ctrl+d at the end of the file

Hello tamil and Hello world

file is opened successfully


the contents are writtenfile closed[root@linuxserver record]# cat tamil

Hello tamil and Hello world


PROGRAM:

/*SIMULATION OF LS COMMAND*/

#include<stdio.h>
#include<sys/types.h>
#include<dirent.h>
main(int argc,char *argv[])
{
DIR *dir;
struct dirent *rddir;
printf("\n enter directory with output command");
dir=opendir(argv[1]);
while((rddir=readdir(dir))!=NULL)
{
printf("%s\t",rddir->d_name);
}
close(dir);
}
OUTPUT:

dshare2.c4038 new a.out .. shmget.c


file3hdhgdhgdhgdgdhfsxgf dir1 s .bash_logout vignesh mr
.lesshstirectorypr.ch oustat.comnn.txt .kde jj write.c producerconsumer
.c waite.c lsc .bashrc create.c madhuy .zshrc .viminfo
oi.c balaji m1.c fl2 ipc.c hex . ls.c f2 newfile
tamil.c grep.c .bash_history fl1.c fl1 fs.c producer.c wait.c
PROGRAM:

/*SIMULATION OF GREP COMMAND*/

#include<stdio.h>
#include<stdlib.h>
main()
{
FILE *fp;
char c[100],pat[10];
int l,i,j=0,count=0,len,k,flag=0;
printf("\n enter the pattern");
scanf("%s",pat);
len=strlen(pat);
fp=fopen("tamil.c","r");
while(!feof(fp))
{
fscanf(fp,"%s",c);
l=strlen(c);
count++;
for(i=0;i<1;i++)
{
if(c[i]=pat[j])
{
flag=0;
for(k=1;k<len;k++)
{
if(c[i+k]!=pat[k])
flag=1;
}
if(flag==0)
printf("\n the pattern %s present in word %d",pat,count);
}
}
}
}
OUTPUT:
enter the pattern sd

the pattern sd present in word 3


the pattern sd present in word 4
PROGRAM:

/*PRODUCER CONSUMER PROBLEM(PCP)*/

#include<stdio.h>
#include<semaphore.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/mman.h>
#include<sys/types.h>
#include<signal.h>
#include<stdlib.h>

#define EMPTY -2
#define TERMINATE -1

int main()
{

sem_t* mutex;

sem_unlink("/mutex_lab");

int temp;

mutex = (sem_t*)sem_open("/mutex_lab",O_CREAT,S_IRWXU|S_IRWXG|
S_IRWXO,1); //READ WRITE EXECUTE (RWX);

int* buffer = mmap(0, sizeof(int)*50, PROT_READ|PROT_WRITE,MAP_SHARED|


MAP_ANONYMOUS, -1, 0);
*buffer = EMPTY;

int pid = fork();

setbuf(stdout,0);

if (pid != 0)
while(1)
{
sem_wait(mutex);
temp = *buffer;

printf("\n\n\n<---------------PRODUCER------------->");
printf("\n(ENTER -1) IN THE PRODUCTION VALUE TO TERMINATE THE
PROGRAM\n");

if(temp == EMPTY)
{
printf("\nEnter the data to be produced!!!!!...\n");
scanf("%d",&temp);

if(temp==TERMINATE)
{

printf("\nPROCESS(ES) TERMINATING NOW!!!!!!!....");


kill(0,SIGTERM);
exit(1);
}

printf("\nProduced...{ %d }\n",temp);
*buffer = temp;
}
else
printf("\n {FULL} BUFFER FULL!!!Producer is Waiting For the Consumer...\n\n");

sem_post(mutex);

if(rand()%2)
sleep(1);
}

else
while(1)
{
sem_wait(mutex);

temp = *buffer;

printf("\n\n\n<-----------------CONSUMER------------------>");

if(temp==EMPTY)
printf("\n{ EMPTY } No data....consumer is waiting for Producer...\n");
else
printf("\nTHE DATA CONSUMED IS....%d. Now buff is {EMPTY}",temp);

*buffer = temp = EMPTY;

sem_post(mutex);

if(rand()%2)
sleep(1);
}

}
OUTPUT:
[root@linuxserver record]$ cc pcs15.c –lpthread
[root@linuxserver record]$ ./a.out

<-----------------CONSUMER------------------>
{ EMPTY } No data....consumer is waiting for Producer...

<---------------PRODUCER------------->
(ENTER -1) IN THE PRODUCTION VALUE TO TERMINATE THE PROGRAM
Enter the data to be produced!!!!!...
5
Produced...{ 5 }

<-----------------CONSUMER------------------>
THE DATA CONSUMED IS....5. Now buff is {EMPTY}

<-----------------CONSUMER------------------>
{ EMPTY } No data....consumer is waiting for Producer...

<---------------PRODUCER------------->
(ENTER -1) IN THE PRODUCTION VALUE TO TERMINATE THE PROGRAM
Enter the data to be produced!!!!!...
3
Produced...{ 3 }

<---------------PRODUCER------------->
(ENTER -1) IN THE PRODUCTION VALUE TO TERMINATE THE PROGRAM
{FULL} BUFFER FULL!!!Producer is Waiting For the Consumer...

<-----------------CONSUMER------------------>
THE DATA CONSUMED IS....3. Now buff is {EMPTY}

<---------------PRODUCER------------->
(ENTER -1) IN THE PRODUCTION VALUE TO TERMINATE THE PROGRAM
Enter the data to be produced!!!!!...
4
Produced...{ 4 }

<-----------------CONSUMER------------------>
THE DATA CONSUMED IS....4. Now buff is {EMPTY}

<---------------PRODUCER------------->
(ENTER -1) IN THE PRODUCTION VALUE TO TERMINATE THE PROGRAM
Enter the data to be produced!!!!!...
-1
PROCESS(ES) TERMINATING NOW!!!!!!!....Terminated
PROGRAM:

/*IPC SHARED MEMORY1 (SENDER)*/

#include<sys/types.h>
#include<sys/msg.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include <stdio.h>
#define K 1024
int main()
{
int shmid;
char *addr1;
key_t key;
key = ftok("/home/cs4038/share.c",'R');
shmid = shmget(key,128*K,IPC_CREAT|SHM_R|SHM_W);
addr1=shmat(shmid,0,0);
printf("\n IPC using shared memory \n");
printf("\n SENDER ADDRESS\n");
printf("\n the address is %p",addr1);
printf("\n enter the message:");
scanf("%s",addr1);
printf("\n MESSAGE STORED IN %p is %s",addr1,addr1);
return 0;
}
PROGRAM:

/*IPC SHARED MEMORY2 (RECIEVER)*/

#include<sys/types.h>
#include<sys/msg.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>

#define K 1024
main()
{
int shmid;
char *addr1;
key_t key;
key = ftok("/home/cs4038/share.c",'R');
shmid=shmget(key,128*K,SHM_R|SHM_W);
addr1=shmat(shmid,0,0);
printf("\n ipc using shared memory");
printf("\n receiver address\n");
printf("\n the address is %p",addr1);
printf("\n message retrieved from %pi is %s",addr1,addr1);
}
OUTPUT:

[root@linuxserver record]$ cc share.c


[root@linuxserver record] $./a.out

IPC using shared memory

SENDER ADDRESS

the address is 0x2b48307c9000


enter the message:Beautiful

MESSAGE STORED IN 0x2b48307c9000 is Beautiful

[root@linuxserver record]# cc share2.c


[root@linuxserver record]# ./a.out

ipc using shared memory


receiver address

the address is 0x2b88c8382000


message retrieved from 0x2b88c8382000i is Beautiful

Vous aimerez peut-être aussi