Advertisement
coolved1543

USP Lab Exam Today

Jan 14th, 2025
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.58 KB | Source Code | 0 0
  1. Write a program to read a file and display it on the console
  2. #define _POSIX_SOURCE
  3. #define _POSIX_C_SOURCE 199309L
  4. #include<stdio.h>
  5. #include<unistd.h>
  6. #include<fcntl.h>
  7. #include<sys/types.h>
  8. int main(int argc,char* argv[])
  9. {
  10. int fd1,n;
  11. char buf1[2];
  12. if(argc<2)
  13. {
  14. perror("Too few arguements...");
  15. return 0;
  16. }
  17. fd1=open(argv[1],O_RDONLY);
  18.  if(fd1==-1)
  19. {
  20. perror(" "); return 0;
  21. }
  22. while(n=read(fd1,buf1,1)>0)
  23. write(1,buf1,1);
  24. //printf("%c",buf1);
  25. return 0;
  26. }
  27. Write a program to create and write Hello world to a file.
  28. // C program to illustrate
  29. // write system Call
  30. #include<stdio.h>
  31. #include <fcntl.h>
  32. #include<string.h>
  33. int main()
  34. {
  35. int sz;
  36. int fd = open("foo.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
  37. if (fd < 0)
  38. {
  39.     perror("r1");
  40.     return(0);
  41. }
  42. sz = write(fd, "hello world\n", strlen("hello world\n"));
  43. close(fd);
  44. return(0);
  45. }
  46. Write a program to find the type of the file given as an input
  47. #define _POSIX_SOURCE
  48. #define POSIX_C_SOURCE 199309L
  49. #include<stdio.h>
  50. #include<unistd.h>
  51. #include<fcntl.h>
  52. #include<sys/types.h>
  53. #include<sys/stat.h>
  54. #include<string.h>
  55. int main(int argc, char* argv[])
  56. {
  57. struct stat statv; char c[100];
  58. if(stat(argv[1],&statv)==0)
  59. printf("File exists\n");
  60.  else
  61. {
  62.   printf("No file");
  63.    return 0;
  64. }
  65. if(S_ISDIR(statv.st_mode))
  66.  printf("Directory file\n");
  67. if(S_ISCHR(statv.st_mode))
  68.   printf("Character file\n");
  69. if(S_ISBLK(statv.st_mode))
  70.   printf("Block file\n");
  71. if(S_ISLNK(statv.st_mode))
  72.   printf("Symbolic link file\n");
  73. if(S_ISFIFO(statv.st_mode))
  74.   printf("FIFO or pipe\n");
  75. if(S_ISREG(statv.st_mode))
  76.   printf("Regular file\n");
  77. return 0;
  78. }
  79. 4. Write a C program that takes one or more file/directory names as command line  
  80.      input and reports the following information on the file:
  81. File type
  82. Number of links
  83. Time of last access
  84. Size of the file
  85. Inode number
  86. #define _POSIX_SOURCE
  87. #define POSIX_C_SOURCE 199309L
  88. #include<stdio.h>
  89. #include<unistd.h>
  90. #include<fcntl.h>
  91. #include<sys/types.h>
  92. #include<sys/stat.h>
  93. #include<string.h>
  94. int main(int argc, char* argv[])
  95. {
  96. struct stat statv; char c[100];
  97. if(stat(argv[1],&statv)==0)
  98. printf("File exists\n");
  99.  
  100. else
  101. {
  102. printf("No file"); return 0;
  103. }
  104. if(S_ISDIR(statv.st_mode))
  105.   printf("Directory file\n");
  106. if(S_ISCHR(statv.st_mode))
  107.    printf("Character file\n");
  108. if(S_ISBLK(statv.st_mode))
  109.    printf("Block file\n");
  110. if(S_ISLNK(statv.st_mode))
  111.   printf("Symbolic link file\n");
  112. if(S_ISFIFO(statv.st_mode))
  113.   printf("FIFO or pipe\n");
  114. if(S_ISREG(statv.st_mode))
  115.   printf("Regular file\n");
  116. printf("Number of links: %d \n",statv.st_nlink);
  117. printf("User id: %d\n",statv.st_uid);
  118.  printf("Group id: %d\n",statv.st_gid);
  119. printf("size of the file is %d\n",statv.st_size);
  120. printf("Inode numberof the file is %d",statv.st_ino);
  121. return 0;
  122.  
  123. }
  124. 5.Implement in C the following UNIX commands using system calls a) Cat b) mv
  125. # include<stdio.h>
  126. //# define PERROR -1
  127. int main(int argc, char* argv[])
  128. {
  129. char op;
  130. if((argc<3)||(argc>4))
  131. {
  132. fprintf(stderr,"ERROR");
  133. return(0);
  134. }
  135. else
  136. {
  137. if(link(argv[1],argv[2])==-1)
  138. {
  139. perror(argv[0]);
  140. }
  141. if(unlink(argv[1])==-1)
  142. {
  143. perror(argv[1]);
  144. }
  145. }
  146. printf("sucessfull");
  147. return(0);
  148. }
  149. 6.Implement in C the following UNIX commands using system calls a) cp b)ln
  150. Cp
  151. #define _POSIX_SOURCE
  152. #define _POSIX_C_C_SOURCE 199309L
  153.  #include<stdio.h>
  154. #include<unistd.h>
  155. #include<fcntl.h>
  156.  #include<sys/types.h>
  157. #include<string.h>
  158. int main()
  159. {
  160. int fd1,n,fd2,n1;
  161. char buf1[50],buf2[50],buf3[50],buf4[50];
  162.  
  163. fd2=open("file2.txt",O_WRONLY|O_CREAT|O_APPEND,777);
  164. fd1=open("file1.txt",O_RDONLY);
  165. if(fd1==-1 || fd2==-1)
  166. {
  167. perror(" ");
  168. return 0;
  169. }
  170. while(n1=read(fd1,buf3,10)>0)
  171. write(fd2,buf3,10);
  172. close(fd2);
  173. lseek(fd1,0,SEEK_SET);
  174.  
  175. close(fd1);
  176. return 0;
  177. }
  178. LN
  179. #define _POSIX_SOURCE
  180. #define _POSIX_C_SOURCE 199309L
  181. #include<stdio.h>
  182. #include<unistd.h>
  183. #include<fcntl.h>
  184. #include<sys/types.h>
  185. int main(int argc, char* argv[])
  186. {
  187. int fd,fd1;
  188. if(argc<3)
  189. {
  190. perror("Too less arguements... ");
  191. return 0;
  192. }
  193. fd=link(argv[1],argv[2]);
  194. if(fd==0)
  195.   printf("Linking successful\n");
  196. else
  197.   printf("Linking Unsuccessful\n");
  198. //fd1=unlink(argv[1]); if(fd1==0)
  199. //printf("Unlinking Successful\n");
  200. //else
  201. //printf("Unlinking unsuccessful\n");
  202. return 0;
  203. }
  204. 7.Write to read form key board and print it on the console using read write API
  205. #define _POSIX_SOURCE
  206. #define _POSIX_C_SOURCE 199309L
  207. #include<stdio.h>
  208. #include<unistd.h>
  209. #include<fcntl.h>
  210. #include<sys/types.h>
  211. int main(int argc,char* argv[])
  212. {
  213. int fd1,n;
  214. char buf1[2];
  215. while(n=read(0,buf1,1)>0)
  216. write(1,buf1,1);
  217. //printf("%c",buf1);
  218. return 0;
  219. }
  220. 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.
  221. #define _POSIX_SOURCE
  222. #define _POSIX_C_SOURCE 199309L
  223. #include<stdio.h>
  224. #include<unistd.h>
  225. #include<fcntl.h>
  226. #include<sys/types.h>
  227. #include<sys/stat.h>
  228. int main()
  229. {
  230. char c[2];
  231. int fd1=open("file1.txt",O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU|S_IROTH| S_IRGRP|S_IXGRP);
  232. if(fd1==-1)
  233. {
  234.  perror(" ");
  235. return 0;
  236. }
  237. else
  238. {
  239. printf(" File created..\n");
  240. write(fd1,"We are quarantined since one month",35);
  241. lseek(fd1,48,SEEK_CUR);
  242. write(fd1," We are not sure when lockdown will be released",47);
  243. close(fd1);
  244. //fd1=open("file1.txt",O_RDONLY);
  245. // while(read(fd1,c,1)>0)
  246. //printf("%s",c);
  247. //printf("\n");
  248. return 0;
  249. }
  250. }
  251. 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
  252. #include<stdio.h>
  253. #include<unistd.h>
  254. int main()
  255. {
  256. int a=fork();
  257. if(a<0)
  258. printf("Child process creation unsuccessful...\n");
  259. if(a==0)
  260. printf("Child written by child process whose process id is: %d and its parent process has id: %d \n",getpid(),getppid());
  261. if(a>0)
  262. printf("Parent written by parent process with process id is: %d\n",getpid());
  263. return 0;
  264. }
  265. 10. Write a program to display the area of the circle in the child's process and the find
  266.     the area of a square in the parent process.
  267. #include<stdio.h>
  268. #include<stdlib.h>
  269. #include<unistd.h>
  270. int main()
  271. {
  272. int l,r;
  273. float area,pi=3.142;
  274. int a=vfork();
  275. if(a<0)
  276. printf("Child process creation unsuccessful \n");
  277. if(a==0)
  278. {
  279. printf("Enter the radius of the circle: ");
  280. scanf("%d",&r);
  281. area=pi * r * r;
  282. printf("Area of the circle is: %f square units\n",area);
  283. printf("It is created by the child process whose id is: %d and its prent has the process id: %d\n",getpid(),getppid());
  284. //exit(0);
  285. }
  286. printf("Enter the length of side of a square:");
  287. scanf("%d",&l);
  288. area=l*l;
  289. printf("Area of square is: %f square units\n",area);
  290. printf("It is calculated by the parent process whose process id is: %d\n",getpid()); return 0;
  291. }
  292.  
  293. 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.
  294.  
  295. #include <iostream>
  296. #include <unistd.h>
  297. using namespace std;
  298. int main()
  299. {
  300.    int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  301.    int sumOdd = 0, sumEven = 0, n, i;
  302.    n = fork();
  303.    if (n > 0) {
  304.        for (i = 0; i < 10; i++) {
  305.            if (a[i] % 2 == 0)
  306.                sumEven = sumEven + a[i];
  307.        }
  308.        cout<<"Parent process\n";
  309.        cout<<"Sum of even no. is "<<sumEven<<endl;
  310.    }
  311.  
  312.    // If n is 0 i.e. we are in child process
  313.    else {
  314.        for (i = 0; i < 10; i++) {
  315.            if (a[i] % 2 != 0)
  316.                sumOdd = sumOdd + a[i];
  317.        }
  318.        cout<< "Child process \n";
  319.        cout<< "\nSum of odd no. is " <<sumOdd<<endl;
  320.    }
  321.    return 0;
  322. }
  323.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement