Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Write a program to read a file and display it on the console
- #define _POSIX_SOURCE
- #define _POSIX_C_SOURCE 199309L
- #include<stdio.h>
- #include<unistd.h>
- #include<fcntl.h>
- #include<sys/types.h>
- int main(int argc,char* argv[])
- {
- int fd1,n;
- char buf1[2];
- if(argc<2)
- {
- perror("Too few arguements...");
- return 0;
- }
- fd1=open(argv[1],O_RDONLY);
- if(fd1==-1)
- {
- perror(" "); return 0;
- }
- while(n=read(fd1,buf1,1)>0)
- write(1,buf1,1);
- //printf("%c",buf1);
- return 0;
- }
- Write a program to create and write Hello world to a file.
- // C program to illustrate
- // write system Call
- #include<stdio.h>
- #include <fcntl.h>
- #include<string.h>
- int main()
- {
- int sz;
- int fd = open("foo.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
- if (fd < 0)
- {
- perror("r1");
- return(0);
- }
- sz = write(fd, "hello world\n", strlen("hello world\n"));
- close(fd);
- return(0);
- }
- Write a program to find the type of the file given as an input
- #define _POSIX_SOURCE
- #define POSIX_C_SOURCE 199309L
- #include<stdio.h>
- #include<unistd.h>
- #include<fcntl.h>
- #include<sys/types.h>
- #include<sys/stat.h>
- #include<string.h>
- int main(int argc, char* argv[])
- {
- struct stat statv; char c[100];
- if(stat(argv[1],&statv)==0)
- printf("File exists\n");
- else
- {
- printf("No file");
- return 0;
- }
- if(S_ISDIR(statv.st_mode))
- printf("Directory file\n");
- if(S_ISCHR(statv.st_mode))
- printf("Character file\n");
- if(S_ISBLK(statv.st_mode))
- printf("Block file\n");
- if(S_ISLNK(statv.st_mode))
- printf("Symbolic link file\n");
- if(S_ISFIFO(statv.st_mode))
- printf("FIFO or pipe\n");
- if(S_ISREG(statv.st_mode))
- printf("Regular file\n");
- return 0;
- }
- 4. Write a C program that takes one or more file/directory names as command line
- input and reports the following information on the file:
- File type
- Number of links
- Time of last access
- Size of the file
- Inode number
- #define _POSIX_SOURCE
- #define POSIX_C_SOURCE 199309L
- #include<stdio.h>
- #include<unistd.h>
- #include<fcntl.h>
- #include<sys/types.h>
- #include<sys/stat.h>
- #include<string.h>
- int main(int argc, char* argv[])
- {
- struct stat statv; char c[100];
- if(stat(argv[1],&statv)==0)
- printf("File exists\n");
- else
- {
- printf("No file"); return 0;
- }
- if(S_ISDIR(statv.st_mode))
- printf("Directory file\n");
- if(S_ISCHR(statv.st_mode))
- printf("Character file\n");
- if(S_ISBLK(statv.st_mode))
- printf("Block file\n");
- if(S_ISLNK(statv.st_mode))
- printf("Symbolic link file\n");
- if(S_ISFIFO(statv.st_mode))
- printf("FIFO or pipe\n");
- if(S_ISREG(statv.st_mode))
- printf("Regular file\n");
- printf("Number of links: %d \n",statv.st_nlink);
- printf("User id: %d\n",statv.st_uid);
- printf("Group id: %d\n",statv.st_gid);
- printf("size of the file is %d\n",statv.st_size);
- printf("Inode numberof the file is %d",statv.st_ino);
- return 0;
- }
- 5.Implement in C the following UNIX commands using system calls a) Cat b) mv
- # include<stdio.h>
- //# define PERROR -1
- int main(int argc, char* argv[])
- {
- char op;
- if((argc<3)||(argc>4))
- {
- fprintf(stderr,"ERROR");
- return(0);
- }
- else
- {
- if(link(argv[1],argv[2])==-1)
- {
- perror(argv[0]);
- }
- if(unlink(argv[1])==-1)
- {
- perror(argv[1]);
- }
- }
- printf("sucessfull");
- return(0);
- }
- 6.Implement in C the following UNIX commands using system calls a) cp b)ln
- Cp
- #define _POSIX_SOURCE
- #define _POSIX_C_C_SOURCE 199309L
- #include<stdio.h>
- #include<unistd.h>
- #include<fcntl.h>
- #include<sys/types.h>
- #include<string.h>
- int main()
- {
- int fd1,n,fd2,n1;
- char buf1[50],buf2[50],buf3[50],buf4[50];
- fd2=open("file2.txt",O_WRONLY|O_CREAT|O_APPEND,777);
- fd1=open("file1.txt",O_RDONLY);
- if(fd1==-1 || fd2==-1)
- {
- perror(" ");
- return 0;
- }
- while(n1=read(fd1,buf3,10)>0)
- write(fd2,buf3,10);
- close(fd2);
- lseek(fd1,0,SEEK_SET);
- close(fd1);
- return 0;
- }
- LN
- #define _POSIX_SOURCE
- #define _POSIX_C_SOURCE 199309L
- #include<stdio.h>
- #include<unistd.h>
- #include<fcntl.h>
- #include<sys/types.h>
- int main(int argc, char* argv[])
- {
- int fd,fd1;
- if(argc<3)
- {
- perror("Too less arguements... ");
- return 0;
- }
- fd=link(argv[1],argv[2]);
- if(fd==0)
- printf("Linking successful\n");
- else
- printf("Linking Unsuccessful\n");
- //fd1=unlink(argv[1]); if(fd1==0)
- //printf("Unlinking Successful\n");
- //else
- //printf("Unlinking unsuccessful\n");
- return 0;
- }
- 7.Write to read form key board and print it on the console using read write API
- #define _POSIX_SOURCE
- #define _POSIX_C_SOURCE 199309L
- #include<stdio.h>
- #include<unistd.h>
- #include<fcntl.h>
- #include<sys/types.h>
- int main(int argc,char* argv[])
- {
- int fd1,n;
- char buf1[2];
- while(n=read(0,buf1,1)>0)
- write(1,buf1,1);
- //printf("%c",buf1);
- return 0;
- }
- 8.Write a program to create a file with 10 bytes of data from the beginning and 10 bytes of data from offset of 48 and display the contents of the file.
- #define _POSIX_SOURCE
- #define _POSIX_C_SOURCE 199309L
- #include<stdio.h>
- #include<unistd.h>
- #include<fcntl.h>
- #include<sys/types.h>
- #include<sys/stat.h>
- int main()
- {
- char c[2];
- int fd1=open("file1.txt",O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU|S_IROTH| S_IRGRP|S_IXGRP);
- if(fd1==-1)
- {
- perror(" ");
- return 0;
- }
- else
- {
- printf(" File created..\n");
- write(fd1,"We are quarantined since one month",35);
- lseek(fd1,48,SEEK_CUR);
- write(fd1," We are not sure when lockdown will be released",47);
- close(fd1);
- //fd1=open("file1.txt",O_RDONLY);
- // while(read(fd1,c,1)>0)
- //printf("%s",c);
- //printf("\n");
- return 0;
- }
- }
- 9.Write a C program to create a child process and allow the parent to display “parent” and the child to display “child” on the screen
- #include<stdio.h>
- #include<unistd.h>
- int main()
- {
- int a=fork();
- if(a<0)
- printf("Child process creation unsuccessful...\n");
- if(a==0)
- printf("Child written by child process whose process id is: %d and its parent process has id: %d \n",getpid(),getppid());
- if(a>0)
- printf("Parent written by parent process with process id is: %d\n",getpid());
- return 0;
- }
- 10. Write a program to display the area of the circle in the child's process and the find
- the area of a square in the parent process.
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- int main()
- {
- int l,r;
- float area,pi=3.142;
- int a=vfork();
- if(a<0)
- printf("Child process creation unsuccessful \n");
- if(a==0)
- {
- printf("Enter the radius of the circle: ");
- scanf("%d",&r);
- area=pi * r * r;
- printf("Area of the circle is: %f square units\n",area);
- printf("It is created by the child process whose id is: %d and its prent has the process id: %d\n",getpid(),getppid());
- //exit(0);
- }
- printf("Enter the length of side of a square:");
- scanf("%d",&l);
- area=l*l;
- printf("Area of square is: %f square units\n",area);
- printf("It is calculated by the parent process whose process id is: %d\n",getpid()); return 0;
- }
- Write a program that creates the child using fork system call and one process finds the sum of odd series and the other process finds the sum of even series.
- #include <iostream>
- #include <unistd.h>
- using namespace std;
- int main()
- {
- int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
- int sumOdd = 0, sumEven = 0, n, i;
- n = fork();
- if (n > 0) {
- for (i = 0; i < 10; i++) {
- if (a[i] % 2 == 0)
- sumEven = sumEven + a[i];
- }
- cout<<"Parent process\n";
- cout<<"Sum of even no. is "<<sumEven<<endl;
- }
- // If n is 0 i.e. we are in child process
- else {
- for (i = 0; i < 10; i++) {
- if (a[i] % 2 != 0)
- sumOdd = sumOdd + a[i];
- }
- cout<< "Child process \n";
- cout<< "\nSum of odd no. is " <<sumOdd<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement