0% found this document useful (0 votes)
3 views9 pages

Os Practical

The document explains various Unix/Linux commands and system calls, including 'sudo' for executing commands with elevated privileges, 'fork()' for creating new processes, and 'chmod' for changing file permissions. It details the syntax and usage of these commands, as well as the significance of process IDs and ownership commands like 'chown' and 'chgrp'. Additionally, it covers process management using the 'ps' command and input/output redirection techniques.

Uploaded by

meenakrishu123
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)
3 views9 pages

Os Practical

The document explains various Unix/Linux commands and system calls, including 'sudo' for executing commands with elevated privileges, 'fork()' for creating new processes, and 'chmod' for changing file permissions. It details the syntax and usage of these commands, as well as the significance of process IDs and ownership commands like 'chown' and 'chgrp'. Additionally, it covers process management using the 'ps' command and input/output redirection techniques.

Uploaded by

meenakrishu123
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/ 9

● The sudo command allows you to run programs with the security privileges of

another user (by default, as the superuser). It prompts you for your personal
password and confirms your request to execute a command by checking a file,
called sudoers, which the system administrator configures. Using the sudoers
file, system administrators can give certain users or groups access to some or all
commands without those users having to know the root password. It also logs
all commands and arguments so there is a record of who used it for whatand
when.
Syntex -sudo command
The su command allows you to become another user. To use the su command on a
per-command basis, enter:

su user -c command

● The fork() system call is used in Unix-based OS to create a new process by


duplicating the calling process. The new process is an exact copy of the parent
process, with its own address space and memory.

fork() return the following values:

● Negative value - it represents the creation of the child process was


unsuccessful.
● Zero - it represents a new child process is created.
● Positive value - The process ID of the child process to the parent. The
returned process ID is type pid_t defined in sys/types.h. Usually, the process
ID is an integer.
Since the child and parent processes run concurrently, these processes reside in
different memory space.
Main {} function - When a program begins running, the system calls the function main,
which marks the entry point of the program.

● The following header files are included when we use fork()


● $nano forkdemo.c , include these

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
Int main{}
{
printf(“Process id =%d /n ,getpid();
Returns 0;
}
In terminal — sudo apt-get install gcc
$gcc forkdemo.c
$./a.out gives process id
Now
{
fork();
printf(“Process id =%d /n ,getpid();
$../a.out everytime will give new pid

● The following is the syntax of the fork() in Linux or Ubuntu

pid_t fork();
In the syntax, pid_t is the return type, and no arguments are passed to fork().
Whenever the child process is successfully created, the Process ID of the child
process is returned in the parent process, and value 0 will be returned to the
child process itself.
If any error occurs, then -1 is returned to the parent process, and the child
process is not created.

● getppid() : returns the process ID of the parent of the calling


process.

Syntax:
pid_t getppid(void);

● getpid() : returns the process ID of the calling process.


Syntex- pid_t getpid(void);

PS -process status

- The system will display a list of every process that is currently running on your system

- For each process, a unique identification number, known as the PID, is displayed.
- The TTY column shows the terminal device that is associated with each process.
- The TIME column displays the amount of CPU time used by each process.
- The CMD column shows the executable command that was used to start each process.
1. -ax
This option lists all the currently running processes.

2. -ef

This option lists the currently running processes in full format.

3. -u <username>

This option lists the process for the user that you specified.

5. -C <command>

This option lists the process for the command you specified.

6. -p<PID>

This option lists the processes with the (PID) process identity you
specified.

7. -ppid <PPID>

This option lists the processes with the PPID you specified.

Ps -e gives all process including user and system process.

11. Ps-a gives all process of all users excluding processes not
associated with terminal or system process.

12. Ps-l long listing information

13. Ps -u gives process of a user

chmod refers to change mode, a command for changing file access


permissions. Syntex- chmod [options] [mode] [File_name]
+ : add permissions
- : remove permissions

= : Set the permissions to the specified values

1)r-read

2) w-write

3) x-execute

1. U- user
2. G-group
3. o-others
4. a-all

# Permission rwx

0 none 000

1 execute only 001

2 write only 010

3 write and execute 011

4 read only 100

5 read and execute 101

6 read and write 110

7 read, write, and execute 111


chmod u+rwx [file_name] - Read, write and execute permissions

to the file owner:

chmod go-w [file_name] -Remove write permission for the group

and others:

chmod u+rw,go+r [file_name]-Read and write for Owner, and

Read-only for the group and other

`chown` command, short for “change owner,”

Syntex -chown [options] new_owner[:new_group] file(s)

`chown`: The base command.

`options`: Optional flags that modify the behavior of the `chown` command.

`new_owner[:new_group]`: The new owner and optionally the new group. If `new_group` is
omitted, only the owner is changed.

`file(s)`: The file or files for which ownership is to be changed.

1.Using `-c` Option in `chown` to Change File Ownership


2.`Using `-v` Option in `chown` to Change File Ownership

3. To Change the owner of a file in Linux, chown master file1.txt

chgrp
The `chgrp` command in Linux is used to change the group ownership of a
file or directory. All files in Linux belong to an owner and a group. You can set
the owner by using “chown” command, and the group by the “chgrp”
command.

Syntex- chgrp [OPTION]… GROUP FILE…

chgrp [OPTION]… –reference=RFILE FILE…

$sudo addgroup abc

$sudo chgrp abc xyz

1.-c` or `–changes` Option

To describe the action for each File whose group actually changes.

2.v` Option

To describe the action or non-action taken for every File.

Output redirection
We can overwrite the standard output using the ‘>’ symbol

Input redirection

We can overwrite the standard input using the ‘<’ symbol.

You might also like