Learning Journal Unit 2
Learning Journal Unit 2
Operating systems (OS) play a crucial role in bridging user programs with the
hardware. They achieve this by using system calls and managing process
states effectively. This journal explores the categories of system calls, delves
into the functionality of one, and examines process states, including the
challenges of process switching and the dynamics between parent and child
processes.
Categories of System Calls
System calls are interfaces between a program and the OS, allowing user
applications to request services from the kernel. The primary categories of
system calls include:
1. Process Control: These manage processes, enabling creation,
termination, and execution. Examples include fork(), exec(), and exit().
2. File Management: These handle file operations such as open, close,
read, write, and delete. An example is open().
3. Device Management: These manage device interactions, like reading
or writing to hardware devices. The ioctl() system call is commonly
used.
4. Information Maintenance: These retrieve or set system data, such
as getting time or setting the priority of a process. Examples include
getpid() and alarm().
5. Communication: These facilitate inter-process communication (IPC)
using mechanisms like pipes, message queues, and shared memory.
Examples are pipe() and shmget().
One notable system call is fork(). It is used to create a new process, called
the child process, which is a duplicate of the parent process. The fork() call
returns 0 to the child process and the Process ID (PID) of the child to the
parent. For instance:
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid == 0) {
printf("Child process\n");
} else {
printf("Parent process\n");
}
return 0;
}
In this example, the parent and child processes execute simultaneously after
the fork.
Process States in Linux
Linux processes move through several states:
1. New: The process is being created.
2. Running: The process is actively executing instructions.
3. Waiting: The process is waiting for an event or resource.
4. Terminated: The process has completed execution.
5. Zombie: The process has terminated, but its exit status has not been
retrieved by the parent.
For example, consider a program that spawns a child process with fork().
Initially, the child is in the "New" state. Upon execution, it transitions to
"Running." If it requires input, it moves to "Waiting." After completion, it
enters "Terminated." If the parent has not yet read the child's exit status, the
child becomes a "Zombie."
Switching between processes is computationally expensive due to context
switching. During a context switch, the OS saves the current process's state
and loads the state of the next process. This involves overhead from saving
CPU registers, memory management data, and restoring the environment for
the new process.
Parent-Child Process Dynamics
A parent process can stop a child process under certain conditions:
1. Explicit Signal: The parent can send a signal, such as SIGSTOP or
SIGKILL, to halt the child.
2. Resource Control: If the child exceeds resource limits set by the
parent, it can be terminated.
3. Parent Termination: In some cases, if the parent terminates, the
child may also be terminated unless reparented to init.
For instance, a parent process monitoring resource usage can stop a child
using:
#include <signal.h>
#include <sys/types.h>
int main() {
pid_t child_pid = fork();
if (child_pid == 0) {
while (1); // Infinite loop in child
} else {
sleep(2); // Let the child run for 2 seconds
kill(child_pid, SIGSTOP); // Stop the child process
}
return 0;
}
Conclusion
System calls and process states are fundamental to OS functionality. System
calls like fork() facilitate process management, while process states reflect a
process's lifecycle. Context switching, though vital, is resource-intensive, and
parent processes play a pivotal role in controlling child processes.
Understanding these concepts is key to appreciating the efficiency and
complexity of modern operating systems.
References
Arpaci-Dusseau, R. H., & Arpaci-Dusseau, A. C. (2018). Operating
systems: Three easy pieces (1.01 ed.). Retrieved from
https://pages.cs.wisc.edu/~remzi/OSTEP/
Chapman, D. (2021, December 9). What are the 5 Linux process
states? CBT Nuggets. Retrieved from
https://www.cbtnuggets.com/blog/certifications/open-source/what-are-
the-5-linux-process-states
magichat. (2022, June 8). Linux system call in detail. GeeksforGeeks.
Retrieved from https://www.geeksforgeeks.org/linux-system-call-in-
detail/