Course Hero Logo

operating system practiclas.docx - NAME - ankur tyagi(917)...

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 1 - 7 out of 35 pages.

NAME-ankur tyagi(917) keshavsingh(975)CLASS-B Sc(H) COMPUTERSCIENCE III SEMSECTION-ASUBMITTED TO-MRS.RICHA GARG1. WAP to display the following:
a. Kernel versionb. CPU type and modelc. Information on configured memory, amountof free and used memory#include<stdio.h>#include<sys/vfs.h>#include<sys/sysinfo.h>#include<sys/utsname.h>int main(){int r,a,q,p,b;struct sysinfo s1;struct utsname s2;struct statfs s3;r=sysinfo(&s1);q=uname(&s2);p=statfs("./",&s3);a=(1024*1024);b=(a*1024);if(r==0&&q==0&&p==0){printf("\nSystem name:%s",s2.sysname);printf("\nVersion:%s",s2.version);printf("\nRelease:%s",s2.release);printf("\nMachine:%s",s2.machine);printf("\nTotal RAM:%ldMB",(s1.totalram/a));printf("\nFree RAM:%ld MB",(s1.freeram/a));printf("\nUsed RAM:%ld MB",((s1.totalram-s1.freeram)/a));
printf("\nTotal Size of partition:%ldGB\n",(s3.f_bsize*s3.f_blocks)/b);}else{printf("ERROR\n");}return 0;}2. WAP to print file details including owner accesspermissions, file access time, where file name isgiven as a command line argument#include<fcntl.h>#include<sys\stat.h>#include<stdio.h>#include<stdlib.h>intmain(int argc,char *argv[]){struct stat statbuf;int fd;if(argc!=2){printf(“wrongno. of arguments”);exit 0;}fd=open(argv[1],O-RDONLY|O_CREAT,0744);fstat(fd,&statbuf);close(fd);
printf(“Owner uid=%d\n”,statbuf.st_uid);printf(“Group id=%d\n”,statbuf.st_gid);printf(“Access Permission=%d\n”,statbuf.st_mode);printf(“Size=%d\n”,statbuf.st_size);printf(“No. of links=%d\n”,statbuf.st_nlink);printf(“Last access time=%s\n”,ctime(&statbuf.st_atime));printf(“Last modified time=%s\n”,ctime(&statbuf.st_mtime));return 0;}3.WAP to copy a source file into the target file anddisplay the target file using system calls#include<iostream>#include<fcntl.h>#include<stdlib.h>#include<unistd.h>using namespace std;int main(int argc, char *argv[]){if(argc!=3){cout<<"File name not found "<<endl;exit(1);}int fd1=open(argv[1],0);if(fd1<0){cout<<"Error in opening file "<<argv[1]<<endl;exit(1);}int fd2=creat(argv[2], 0666);if(fd2<0){
cout<<"Error in creating file "<<argv[2]<<endl;exit(1);}char buf[500];int count;while(count=read(fd1,buf,sizeof(buf))){write(fd2,buf,count);}close(fd1);close(fd2);return 0;}4.WAP (using fork() and/or exec() commands)where parent and child execute:a. same program, same code#include<iostream>#include<stdio.h>#include<sys/types.h>#include<unistd.h>using namespace std;int main(){pid_t pid;pid=fork();if(pid<0){cout<<"this is a process";}else{cout<<"process continues";}return 0;}b. same program, different code#include<iostream>#include<stdio.h>#include<unistd.h>
#include<sys/types.h>using namespace std;int main(){pid_t pid;pid=fork();if(pid<0){cout<<"new process not entered";}else if(pid==0){cout<<"Child process";}elsecout<<"parent process";return 0;}c. different programs#include<iostream>#include<stdio.h>#include<unistd.h>#include<sys/types.h>using namespace std;int main(){pid_t pid;pid=fork();if(pid<0){cout<<"No process entered";}else if(pid==0){execlp("/bin/ls","ls",NULL);cout<<"child process";}else {cout<<"parent process";}return 0;}d. before terminating, the parent waits forthe child to finish its task

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 35 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Winter
Professor
Tanmoy
Tags

Newly uploaded documents

Show More

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture