0% found this document useful (0 votes)
20 views7 pages

Unit 4 Linux ...

The document provides an overview of process management in Linux, detailing commands like ps, top, htop, and pgrep for viewing and managing processes. It explains process states, scheduling algorithms, and signal handling, alongside examples of creating and managing processes. Additionally, it covers file locking mechanisms and potential issues like deadlocks.

Uploaded by

Anuj Yadav
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)
20 views7 pages

Unit 4 Linux ...

The document provides an overview of process management in Linux, detailing commands like ps, top, htop, and pgrep for viewing and managing processes. It explains process states, scheduling algorithms, and signal handling, alongside examples of creating and managing processes. Additionally, it covers file locking mechanisms and potential issues like deadlocks.

Uploaded by

Anuj Yadav
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/ 7

UNIT 4 LINUX

Viewing Processes in Linux

1. ps Command:
a. ps: Lists the current running processes for the current user.
b. ps -e: Lists all processes running on the system.
c. ps -ef: Provides a detailed view of all processes with additional
information.

Example:

sh
ps -ef

2. top Command:
a. top: Provides a real-time view of running processes, including CPU
and memory usage. It is interactive and allows sorting and filtering.

Example:

sh
top

3. htop Command:
a. htop: An enhanced version of top with a more user-friendly
interface, allowing easier navigation and process management.

Example:

sh
htop
4. pgrep Command:
a. pgrep: Searches for processes by name and returns their PIDs.

Example:

sh
pgrep apache2

Process Table in Linux

• The Process Table is maintained by the kernel and contains an entry for
each active process. Each entry, known as a process control block (PCB),
stores information such as the process ID, state, priority, and resources
used.

System Processes

• System processes are essential for the functioning of the operating


system. Examples include init (or systemd), which is the parent of all
processes and starts other system processes.

Process Scheduling

• Scheduling: The Linux scheduler determines which process runs on the


CPU at any given time, ensuring efficient CPU utilization and fair
resource distribution.
• Scheduling Algorithms:
o Completely Fair Scheduler (CFS): The default scheduler in Linux,
designed to provide fair access to CPU resources.
o Round Robin: A simple scheduling algorithm where each process
gets an equal share of CPU time in a cyclic order.
o Priority Scheduling: Processes are assigned priorities, and the
scheduler selects processes based on their priority.
Viewing Process Schedulers

• You can view and configure the process scheduler using the chrt
command.

Example:

sh
# View scheduling policy and priority for a process with PID
1234
chrt -p 1234

Example Commands

• Viewing All Processes:

sh
ps -aux

• Viewing Processes in a Hierarchical Tree:

sh
pstree

• Killing a Process:

sh
kill <PID>

Linux provides powerful tools for managing processes, ensuring that the
system runs smoothly and efficiently.

Got it! Here's a detailed look at process management and file locking in Linux:
Starting New Processes

1. Fork: Creates a new process by duplicating the current process. Both


parent and child processes continue from the point of the fork call.

pid_t pid = fork();


if (pid == 0) {
// Child process
} else if (pid > 0) {
// Parent process
} else {
// Fork failed
}

2. Vfork: Similar to fork, but designed to be used when the child process
will call exec immediately. It suspends the parent process until the child
executes a new program or exits.
3. Exec: Replaces the current process image with a new process image.

execlp("ls", "ls", "-l", (char *)NULL);

Process States

• Zombie Processes: Processes that have completed execution but still


have an entry in the process table because their exit status has not been
read by the parent process.
• Orphan Processes: Processes whose parent has terminated. These are
adopted by the init process (or systemd in modern systems).
Waiting for a Process

• wait: Causes the parent process to wait until any of its child processes
has terminated.

int status;
pid_t pid = wait(&status);

• waitpid: Allows the parent process to wait for a specific child process to
terminate.

pid_t pid = waitpid(child_pid, &status, 0);

Signals and Signal Handling

• Signal Functions:
o signal(): Sets a function to handle a specific signal.void
handle_signal(int sig) {
// Signal handling code
}
signal(SIGINT, handle_signal);

o kill(): Sends a signal to a process.kill(pid, SIGTERM);

o raise(): Sends a signal to the calling process.raise(SIGINT);

o alarm(): Sets a timer to send SIGALRM after a specified number


of seconds.alarm(10);

o pause(): Suspends the process until a signal is


received.pause();
o abort(): Causes the process to terminate abnormally.abort();

File Locking

• Creating Lock Files:

int fd = open("lockfile", O_CREAT | O_EXCL, 0644);


if (fd == -1) {
perror("Lock file creation failed");
}

• Locking Regions:
o fcntl(): Uses advisory locks to lock regions of a file.struct
flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0; // Lock the entire file
if (fcntl(fd, F_SETLK, &lock) == -1) {
perror("Locking failed");
}

• Deadlocks: Situations where two or more processes are unable to


proceed because each is waiting for the other to release a resource.

Sleep Functions

• sleep: Suspends the execution of the calling process for a specified


number of seconds.sleep(5);
Signal Sets

• Signal Sets: Collections of signals used in signal handling.sigset_t


set;
sigemptyset(&set);
sigaddset(&set, SIGINT);
sigprocmask(SIG_BLOCK, &set, NULL);

You might also like