



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
lab manual containing knowledge of operating system linux that we practice in lab
Typology: Exercises
1 / 6
This page cannot be seen from the preview
Don't miss anything!




System calls provide programs running on the computer an interface to talk with the operating system. When a program needs to ask for a service (for which it does not have permission itself) from the kernel of the operating system it uses a system call. User level processes do not have the same permissions as the processes directly interacting with the operating system. For example, to communicate with and external I/O device or to interact with any other processes, a program has to use system calls.
1. PROGRAM USING SYSTEM CALL fork(), wait(), execve(),exit(),perror(),getpid() and getppid()
AIM: To write the program to create a Child Process using system call fork ().
ALGORITHM:
1. Fork ( ) Used to create new processes. The new process consists of a copy of the address space of the original process. The value of process id for the child process is zero, whereas the value of process id for the parent is an integer value greater than zero. **Syntax: fork ( )
Syntax: execve ( )
3. wait( )
The parent waits for the child process to complete using the wait system call. The wait system call returns the process identifier of a terminated child, so that the parent can tell which of its possibly many children has terminated.
Syntax: wait (NULL)
4. exit( ) A process terminates when it finishes executing its final statement and asks the operating system to delete it by using the exit system call. At that point, the process may return data (output) to its parent process (via the wait system call). **Syntax: exit (0)
PROGRAM CODING:
Example 1:
#include<stdio.h> #include<stdlib.h> #include<unistd.h> void main(int argc,char *arg[]) { printf("%d",argc); argv[0]="/bin/ls"; int pid; pid=fork(); if(pid<0) { printf("fork failed"); exit(1); } else if(pid==0) {
Step 3 : Create structure as stat buff and the variables as integer. Step 4 : Using the for loop, initialization
PROGRAM CODING:
Example 1:
#include<stdio.h> #include<sys/types.h> #include<sys/dir.h> void main(int age,char *argv[]) { DIR *dir; struct dirent *rddir; printf("\n Listing the directory content\n"); dir=opendir(argv[1]); while((rddir=readdir(dir))!=NULL) { printf("%s\t\n",rddir->d_name); } closedir(dir); }
3. PROGRAM USING SYSTEM CALL stat( ), creat(),open( ), stat(),fstat( ),gets() and lseek()
File Permissions and Number?
File permissions determine what you are allowed to do and who is allowed to do it.
Owner Group World
Read
Write
Execute
Read is equal to 4. Write is equal to 2. Execute is equal to 1. No permissions for a user is equal to 0.
PROGRAM CODING :
Example 1:
#include<stdio.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include<fcntl.h> void main() { int fd1,fd2,n; char source[30],ch[5]; struct stat s,t,w; fd1=creat("text.txt",0644); printf("Enter the file to be copied\n"); scanf("%s",source); fd2=open(source,O_RDONLY); if(fd2==-1) { perror("file doesnot exist"); exit(0); } while((n=read(fd2,ch,1))>0) write(fd1,ch,n); close(fd2); stat(source,&s); printf("Source file size=%d\n",s.st_size); fstat(fd1,&t); printf("Destination file size =%d\n",t.st_size); close(fd1); }
Example 2:
#include<stdio.h> #include<unistd.h>