Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views12 pages

OS Lab (Week08)

The document provides an overview of system calls in Linux, detailing their role in facilitating interactions between programs and the operating system. It explains concepts such as user mode and kernel mode, file descriptors, and includes examples of using system calls like open() and fork(). Additionally, it covers process management commands and how to manipulate processes in a Linux environment.

Uploaded by

mazanmangnejo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

OS Lab (Week08)

The document provides an overview of system calls in Linux, detailing their role in facilitating interactions between programs and the operating system. It explains concepts such as user mode and kernel mode, file descriptors, and includes examples of using system calls like open() and fork(). Additionally, it covers process management commands and how to manipulate processes in a Linux environment.

Uploaded by

mazanmangnejo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

What we learned previously

>>> System Call:

01. System call is a way for program to interact with the OS.
02. An Entry point to the OS Kernel

>>> User Mode and Kernel Mode:


User Mode → All programs or request to generate system calls
Kernel Mode → Execution of system calls
>>> Need of System Call
1. write on screen / in file
2. read from user input or file
3. create process and so on.

File Descriptors:
There are 3 standard file descriptors by default in Linux.
File Descriptor Description stdio stream
0 Standard input stdin
1 Standard output stdout
2 Standard error stderr
Open () – System Call
In order to perform any operation like reading / writing a file, we need to open the file and provide a handle to the
kernel.

Output Device (Console / Fd=0 (Predefined value) Input Device (Keyboard)


screen) Fd=1 (Predefined value)
Fd=2 (Predefined value)
Error (Console/File)
Fd=3 (Not predefined value)
/root/test.txt

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
For Example:
fd=open("test.txt",O_RDONLY);
int open(const char *pathname, int flags, mode_t mode);
Example 01:
fd2=open("F2.txt",O_WRONLY|O_APPEND);
Example 02:
fd3=open("F3.txt",O_WRONLY|O_CREAT,0644);
Week 08:

// Program#01: Write a program to read file contents and print them on screen
System Call: open()

//open.c
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
int n,fd;
char b[20];
fd=open("test.txt",O_RDONLY); //opens test.txt in read mode and the file descriptor is saved in integer fd.
printf("The file descriptor of the file is: %d\n”,fd); // the value of the file descriptor is printed.
n=read(fd,b,10);//read 10 characters from the file pointed to by file descriptor fd and save them in buffer (b)
write(1,b,n); //write on the screen from the buffer
}
Week 08:

//Program#02: Write a program to read some content(10 characters) of file F1.txt and write
them into file F2.txt. The contents of file F2.txt should not get deleted or overwritten

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
int n,fd1,fd2;
char b[15];
fd1=open("F1.txt",O_RDONLY);
fd2=open("F2.txt",O_WRONLY|O_APPEND);
n=read(fd1,b,10);
write(fd2,b,n);
}
Week 08:

//Assignment: Copy entire content of one file into another


Week 08:
System call : fork();

The fork() is a system call, it is used to create a child process, and it is duplicate of the calling process (parent).
Parent and Child (both) processes run concurrently
fork()
Child process Parent
fork() syntax:
Process
#include<unistd.h>
pid_t fork(void);

it returns integer valueb (-ne, 0, or +ve)

Simple program: fork()


#include<unistd.h> -ne 0 +ve
#include<stdio.h> error Child Parent
int main()
{ process Process
fork();
printf(“Good Morning\n”);
}
Week 08:
//Program
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
void main()
{
pid_t p=fork(); //call fork();
if(p==0)
{
printf("\n Hi I am child process\n");
}
else
{
printf("\nHi I am Parent Process\n");
}
}

//Program02
#include<stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
pid_t pid; // Create a child proces
pid = fork();
if (pid == 0)
{
// Child process
printf("Child Process: PID = %d, Parent PID = %d\n", getpid(), getppid());
}
else {
// Parent process
printf("Parent Process: PID = %d, Child PID = %d\n", getpid(), pid);
}
}
Week 08:
//wait();
//Program
#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<sys/wait.h>
int main()
{
pid_t p;
printf("before fork\n");
p=fork();
if(p==0)//child
{
printf("I am child having id %d\n",getpid());
printf("My parent's id is %d\n",getppid());
}
else//parent
{
wait(NULL);
printf("My child's id is %d\n",p);
printf("I am parent having id %d\n",getpid());
}
printf("Common\n");
}
Week 08:
Process:

What is program: Program is set instruction

What is process: Program execution is called a process


Process creation / termination
Process table
Process pid → unique int

Process Stat: Running, Stopped, and etc.

How to list the processes

ps command can be used

#ps
#ps aux
#ps PID

# sleep 10

# sleep 20 &
Week 08:
Week 08:
Process:

# sleep 10

# sleep 20 & : push your process into background

# jobs : This command shows number of background processes

# fg %jobnumber

# bg %jobnumber
Week 08:
Process:

Kill Command:
This command terminates running processes on a Linux machine.
To use these utilities, you need to know the PID (process id) of the process you want to kill
Syntax -
kill PID
To find the PID of a process simply type:
ps –ef | grep processname

You might also like