Process ID & Parent Process ID
each process has a PID , a positive integer that uniquely identifies a process on the system.
once PID reached 32767 , the PID counter is reset to 300, rather than 1.
The swapper or scheduler is a system prcoess having a PID of 0. It manages memory allocation for processes, swaps processes from run state to Ready Queue or other and may be to disk.
The init , now systemd is user process having a PID of 1. It is invoked by the kernel at the end of the botting process.
Page daemon now kthreadd is system process having PID of 2. it support the paging of virtual memeory system.
permission modification , SUID
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
uid_t ruid, euid, suid;
getresuid(&ruid, &euid, &suid);
printf("My Real user-ID is: %d\n", (long)ruid);
printf("My Effective user-ID is: %d\n", (long)euid);
printf("My Saved Set-user-ID is: %d\n\n\n", (long)suid);
int rv = setresuid(100, 2000, 3000);
if (rv == -1){
printf ("Error in setting ID\n");
exit(1);
}
getresuid(&ruid, &euid, &suid);
printf("\n\nAfter setuid(2000) the IDs are:\n");
printf("My Real user-ID is: %d\n", (long)ruid);
printf("My Effective user-ID is: %d\n", (long)euid);
printf("My Saved Set-user-ID is: %d\n", (long)suid);
return 0;
}
we are trying to change id's of process , by only allowing root to change id.
we can also user to run this program by add suid bits. notice even though i am not root user i can still run it due to suid bits (small s in x's place).
fork system call
allows one process , the parent, to create a new process , the child
on success , the return value to the child process is 0 and the return value to the parent process is PID of the child.
on failure , a -1 will be returned in the parent context , no child process created, and errno will be set appropriately.
use case = consider a network server; parent waits for a service request from a client. when the request arrives, parent calls fork and let the child handle the request.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int cpid = fork();
if (cpid == 0)
printf("Hello I am child\n");
else
printf("Hello I am parent\n");
return 0;
}
notice parent process get exectued frist because parent's state is already actvie in the CPU and its memory management information is already cached in the h/w TLB, therefore , running the parent first should result in better performance.
forked process share some attributes wtih parent
- Real, effective and save UIDs /GIDs
- Open file descriptors (PPFDT)
- Environment variables
- Present working directory
- Nice value
- File mode creation mask (umask)
- Signal mask and signal dispostion
- Attached shared memory segments
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if(argc != 2){
fprintf(stderr, "Incorrect number of arguments. Pl pass one integer\n");
return 1;
}
pid_t cpid;
int n = atoi(argv[1]);
int i;
for (i=1;i<=n;i++){
fork();
}
// fflush(0);
fprintf(stderr,"PID=%ld, PPID=%ld\n",(long)getpid(), (long)getppid());
while(1);
return 0;
}
at first there will be one child process (left) and one parent process.
after second iteration child(left) will fork another child process (left below) and parent will fork another child process(right).
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if(argc != 2){
fprintf(stderr, "Incorrect number of arguments. Pl pass one integer\n");
return 1;
}
int n = atoi(argv[1]);
int i;
for (i=1;i<=n;i++)
{
int temp = fork();
fprintf(stderr,"temp is %d and i is %d PID=%ld ,,,," ,temp,i,(long)getpid());
if(temp != 0)//parent should break
break;
}
fprintf(stderr, "PID=%ld, PPID=%ld\n",(long)getpid(), (long)getppid());
while(1);
return 0;
}
notice when fork success parent process will get child's PID for return.
in child process it begins at "int temp = fork()" and will return 0 since it is child process.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
int main(int argc, char *argv[])
{
if(argc != 2){
fprintf(stderr, "Incorrect number of arguments. Pl pass one integer\n");
return 1;
}
int n = atoi(argv[1]);
int i;
for (i=1;i<=n;i++)
{
int temp = fork();
fprintf(stderr,"temp is %d and i is %d PID=%ld ,,,," ,temp,i,(long)getpid());
if(temp == 0){
break;
}
}
fprintf(stderr, "PID=%ld, PPID=%ld\n",(long)getpid(), (long)getppid());
while(1);
return 0;
}
notice first parent calls fork system call 3 times and inside a child process fork() returns 0 so for loop breaks.
'Operating System > System Programming(Arif Butt)' 카테고리의 다른 글
Lec19) Process Management-III (0) | 2021.07.11 |
---|---|
Lec18) Process Management-II (0) | 2021.07.10 |
Lec16) Programming Terminal Devices (0) | 2021.07.08 |
Lec15) Design and Code Of UNIX who Utility , Buffering (0) | 2021.07.06 |
Lec14) Design and Code of UNIX ls Utility (0) | 2021.07.05 |