Sie sind auf Seite 1von 11

PROGRAM: //I/O SYSTEM CALLS #include<stdio.h> #include<unistd.h> #include<fcntl.h> #include<sys/types.h> #include<sys/uio.h> #include<sys/stat.

h> int main()


{

int fd=0; fd=creat("test3",0644); //CREATING A FILE int fdo; fdo=open("test3",O_RDWR,0); //OPENING THE CREATED FILE char a[50]; printf(\nfile 1 is created and opened); printf("\nenter the input for file 1"); scanf("%s",a); //GETTING DATA FOR THE FILE 1 int c; c=strlen(a); write(fd,a,c); //WRITING THE DATA INTO THE FILE printf(\ndata is written into file 1); read(fd,a,c); //READING THE DATA WRITTEN printf(\ncontent from file 1 is read); int fdo1,fd1; fd1=creat("test4",0444); //CREATING 2ND NEW FILE chmod("test4",0644); //CHANGING THE ACCESS MODE OF THE CREATED FILE fdo1=open("test4",O_RDWR); //OPENING THE 2ND FILE write(fdo1,a,c); printf(\nfile 2 is created and the data is written into it); //WRITING THE DATA IN 2ND FILE WHICH IS READ FROM FILE 1 int opt; printf("\nwanna delete file1???"); scanf("%d",&opt);

if(opt==1) { unlink("test3"); //DELETING THE CREATED FILE printf(\nfile 1 is deleted); } close(fdo1); //CLOSING THE OPENED FILES close(fdo); } OUTPUT: $./a.out file 1 is created and opened nenter the input for file 1 yellow data is written into file 1 content from file 1 is read file 2 is created and the data is written into it wanna delete file1??? 1 file 1 is deleted $cat test3 cat: sample1.txt: No such file or directory $cat test 4 yellow PROGRAM: //FORK SYSTEM CALL #include<stdio.h> #include<unistd.h> #include<fcntl.h> #include<sys/types.h> #include<sys/uio.h> #include<sys/stat.h> int main()

{ int c; c=fork(); if(c==0) { printf("\nchild process is created which follows parent"); printf(\nHAVE A NICE DAY!!!); printf(\nchild process ends); sleep(5); } else if(c==-1) { printf("\nerror in child creation"); } else { wait(c); printf("\nparent process"); } } PROGRAM: //EXEC SYSTEM CALL #include<stdio.h> #include<sys/types.h> #include<fcntl.h> #include<unistd.h> #include<sys/uio.h> #include<sys/stat.h> int main() { int id,i; id=fork(); if(id==0) { execl("./pr","pr",NULL); } else if(id==-1) { printf("\nError in creation");

} else { printf("\nParent is working to bid u good day!!! :P"); printf("\n HI HAVE A GOOD DAY!!!"); } } OUTPUT: $ gcc -o pr pr.c $ gcc syscall.c $ ./a.out Parent is working to bid u good day!!! :P HI HAVE A GOOD DAY!!! $ Child is working to say helloHI HELLO :) PROGRAM: //SYSTEM CALLS TO GET IDs #include<stdio.h> #include<unistd.h> #include<fcntl.h> #include<sys/types.h> #include<sys/uio.h> #include<sys/stat.h> int main() { printf("process id of current running process is %d",getpid()); printf("\nthe user id is %d",getuid()); printf("\nthe group id is %d",getgid()); printf("\neffective groupid is %d",getegid()); printf("\nparent process id is %d",getppid()); printf("\nprocess group id is %d",getpgrp()); printf("\neffective user id is %d",geteuid()); }

OUTPUT: process id of current running process is 2390 the user id is 1000 the group id is 1000 effective groupid is 1000 parent process id is 2310 process group id is 2390 effective user id is 1000
PROGRAM: //SLEEP SYSTEM CALL #include<stdio.h> #include<sys/types.h> #include<sys/uio.h> #include<sys/stat.h> #include<fcntl.h> #include<unistd.h> int main() { int i=0,pid; pid=fork(); if(pid==0) { printf("\nchild process starts\n"); for(i=0;i<5;i++) { printf("%d\t",i); }

printf("\nchild process ends\n"); } else { sleep(5); printf("\nparent process\n"); } }

OUTPUT: child process starts 0 1 2 3 4

child process ends parent process

PROGRAM: //WAIT SYSTEM CALL #include<stdio.h> #include<sys/types.h> #include<sys/uio.h> #include<sys/stat.h> #include<fcntl.h> #include<unistd.h> int main() { int i=0,pid;

pid=fork(); if(pid==0) { printf("\nchild process starts\n"); for(i=0;i<5;i++) { printf("%d\t",i); } printf("\nchild process ends\n"); } else { wait(5); printf("\nparent process\n"); } }

OUTPUT: child process starts 0 1 2 3 4

child process ends parent process

PROGRAM: #include<stdio.h> #include<unistd.h> #include<fcntl.h> #include<sys/types.h> #include<sys/uio.h>

#include<sys/stat.h> #include<dirent.h> int main() { struct stat f; int a; a=stat("/usr/fork.c",&f); if(a==0) { printf("file size in bytes is %ld",f.st_size); printf("\nLast status change: %s",ctime(&f.st_ctime)); printf("\nLast file access: %s", ctime(&f.st_atime)); printf("\nLast file modification: %s", ctime(&f.st_mtime)); printf("\ninode no. is %ld",f.st_ino); printf("\nfile mode is %lo",(unsigned long)f.st_mode); printf("\nnumber of hard links are %d",f.st_nlink); printf("\nuser id is %d",f.st_uid); printf("\ngroup id is %d",f.st_gid); } else printf("error!!!"); } OUTPUT: file size in bytes is 425 Last status change: Tue Jan 10 14:03:09 2012 Last file access: Tue Jan 10 14:03:17 2012 Last file modification: Tue Jan 10 14:03:09 2012 inode no. is 2229183 file mode is 100644 number of hard links are 1 user id is 1000 group id is 1000 PROGRAM: #include<stdio.h> #include<unistd.h> #include<fcntl.h> #include<sys/types.h>

#include<sys/uio.h> #include<sys/stat.h> #include<dirent.h> DIR *d; struct dirent *a; int main() { printf("enter the directory name..."); char dname[50]; scanf("%s",dname); d=opendir(dname); if(d==NULL) { printf("\nerror!!!"); } else { printf("\nenter the file name to be searched..."); char filename[50]; scanf("%s",filename); int flag=0; while((a=readdir(d))!=NULL) { if(strcmp(a->d_name,filename)==0) { flag=1; printf("\nthe file is found!!!"); break; } } if(flag==0) { printf("\nthe file is not found"); } } }

OUTPUT: $ ./a.out enter the directory name...shell_programs enter the file name to be searched...fact.sh the file is found!!! $ ./a.out enter the directory name...shell_programs enter the file name to be searched...lajfo the file is not found

Das könnte Ihnen auch gefallen