You are on page 1of 25

Basic Linux commands

Introduction to Linux
Linux is a free and open-source operating system (OS) widely used for desktops,
servers, and other electronic devices. An operating system is software that
manages all of the hardware resources associated with your desktop or laptop. To
put it simply, the operating system manages the communication between your
software and your hardware. Without the operating system (OS), the software
wouldn’t function. Unlike Windows or macOS, Linux's source code is freely
available for anyone to see, modify, and distribute. This fosters a large community
of developers who contribute to its improvement.
Basic commands:
ls
The ls command is used to list files or directories in Linux and other Unix-based operating systems.

pwd
The pwd command writes to standard output the full path name of your current directory from the root
directory.

cd
The cd command in Linux stands for change directory. It is used to change the current directory of the
terminal.
mkdir
The mkdir command is used to create one or more directories specified by the Directory parameter.

rmdir
The rmdir command is used to remove an empty folder.

touch
The touch command is a standard command used in UNIX/Linux operating system which is used to
create, change and modify timestamps of a file.

cat
The cat command reads each File parameter in sequence and writes it to standard output.

rm
The rm command is used to remove a file.
uname
The uname command to get basic information about the OS.

clear
The clear command is used to clear the terminal.

ps
The ps command is used to display processes in terminal.

man
The man command is used to access manual for all linux commands.

df
The df command is used to check the details of the file system.
wc
The wc command is used to check the lines, word count, and characters in a file.
Program to print process ID
#include<stdio.h>

#include<unistd.h>

int main()

int pid,ppid;

pid = getpid();

ppid = getppid();

printf("The Process ID is %d \n", pid);

printf("The Parent Process ID is %d \n", ppid);

while (1)

{}

return 0;

Output
Program to create single process
#include<stdio.h>

#include <sys/types.h>

#include <unistd.h>

int main()

printf("This is to demonstrate the fork\n");

fork();

printf("I am Nikhil lamsal.\n");

return 0;

Output
Program to create multiple process
#include <stdio.h>

#include <unistd.h>

int main()

int p1, p2, p3;

printf("\n Parent's pid=%d\n", getpid());

p1 = fork();

if (p1 == 0) {

printf("\n Child 1's pid=%d\n", getpid());

printf("\n Child 1's parent's pid=%d\n", getppid());

p2 = fork();

if (p2 == 0) {

printf("\n Child 2's pid=%d\n", getpid());

printf("\n Child 2's parent's pid=%d\n", getppid());

p3 = fork();

if (p3 == 0) {

printf("\n Child 3's pid=%d\n", getpid());

printf("\n Child 3's parent's pid=%d\n", getppid());

return 0;

}
Output

Program to check process state


#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

int main()

int pid = fork();

if (pid > 0) {

printf("\nParent's ID P : %d\n", getpid());

else if (pid == 0) {

printf("\nBEFORE\n");

printf("Child's ID: %d\n", getpid());

printf("Parent -ID: %d\n\n", getppid());

printf("\nAFTER\n");

printf("\nChild ID: %d\n", getpid());

printf("Parent -ID: %d\n", getppid());


}

else {

printf("Failed to create child process");

while (1)

{}

return 0;

Output
Program to create thread
#include <pthread.h>

#include <stdio.h>

#include <stdlib.h>

#define NO_OF_THREADS 4

void *print_hello_world(void *tid)

// This function prints thread's identifier and then exits

printf(" thread count= %d\n\n", *((int *)tid));

// castingvoidpointer

pthread_exit(NULL);

int main()

// The main program creates 5 threads

pthread_t threads[NO_OF_THREADS];

int status, i;

for (i = 1; i <= NO_OF_THREADS; i++)

printf("Creating thread %d\n", i);

pthread_create(&threads[i], NULL, &print_hello_world, &i);

pthread_join(threads[i], NULL);

while(1)

{}

return 0;

}
Output
Program to demonstrate mutual exclusion
#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<pthread.h>

void *thread1f(void *arg);

void *thread2f(void *arg);

int turn=1;

int main()

pthread_t thid1;

pthread_t thid2;

pthread_create(&thid1,NULL,&thread1f,NULL);

pthread_create(&thid2,NULL,&thread2f,NULL);

pthread_join(thid1,NULL);

pthread_join(thid2,NULL);

return 0;

void *thread1f(void *arg)

int a=0;

while(a++<20){

fputc('n',stderr);

void *thread2f(void *arg)

int a=0;

while(a++<20){
fputc('b',stderr);

Output
Program to implement Producer Consumer
problem using mutex
#include <stdio.h>

#include <stdlib.h>

int mutex=1,full=0,empty=3,x=0;

int main()

int n;

void producer();

void consumer();

int wait(int);

int signal(int);

printf("\n1. Producer \n2.Consumer \n3.Exit");

while(1){

printf("\nEnter your choice:");

scanf("%d",&n);

switch(n){

case 1:

if((mutex==1)&&(empty!=0))

producer();

else

printf("Buffer is full");

break;

case 2:

if((mutex==1)&&(full!=0))

consumer();

else

printf("Buffer is empty");
break;

case 3:

exit(0);

break;

return 0;

int wait(int s){

return(--s);

int signal(int s){

return(++s);

void producer(){

mutex=wait(mutex);

full=signal(full);

empty=wait(empty);

x++;

printf("\nProducer produces the item %d",x);

mutex=signal(mutex);

void consumer(){

mutex=wait(mutex);

full=wait(full);

empty=signal(empty);

printf("\n Consumer consumes the item %d",x);

x--;

mutex=signal(mutex);
}

Output
Program to implement Dining Philosopher
Problem

#include<stdio.h>

#include<stdlib.h>

#include<pthread.h>

#include<semaphore.h>

#include<unistd.h>

sem_t chopstick[5];

void * philos(void *);

void eat(int);

int main()

int i,n[5];

pthread_t T[5];

for(i=0;i<5;i++)

sem_init(&chopstick[i],0,1);

for(i=0;i<5;i++){

n[i]=i;

pthread_create(&T[i],NULL,philos,(void *)&n[i]);

for(i=0;i<5;i++)

pthread_join(T[i],NULL);

void * philos(void * n)

int ph=*(int *)n;


printf("Philosopher %d wants to eat\n",ph);

printf("Philosopher %d tries to pick left chopstick\n",ph);

sem_wait(&chopstick[ph]);

printf("Philosopher %d picks the left chopstick\n",ph);

printf("Philosopher %d tries to pick the right chopstick\n",ph);

sem_wait(&chopstick[(ph+1)%5]);

printf("Philosopher %d picks the right chopstick\n",ph);

eat(ph);

sleep(2);

printf("Philosopher %d has finished eating\n",ph);

sem_post(&chopstick[(ph+1)%5]);

printf("Philosopher %d leaves the right chopstick\n",ph);

sem_post(&chopstick[ph]);

printf("Philosopher %d leaves the left chopstick\n",ph);

void eat(int ph)

printf("Philosopher %d begins to eat\n",ph);

}
Output
Program to implement First fit allocation method
#include<stdio.h>

void firstFit(int blockSize[], int m, int processSize[], int n) {

int i, j;

int allocation[n];

for (i = 0; i < n; i++) {

allocation[i] = -1;

for (i = 0; i < n; i++) {

for (j = 0; j < m; j++) {

if (blockSize[j] >= processSize[i]) {

allocation[i] = j;

blockSize[j] -= processSize[i];

break;

printf("\nProcess No.\tProcess Size\tBlock no.\n");

for (int i = 0; i < n; i++) {

printf(" %i\t\t\t", i+1);

printf("%i\t\t\t\t", processSize[i]);

if (allocation[i] != -1)

printf("%i", allocation[i] + 1);

else

printf("Not Allocated");

printf("\n");

}
int main() {

int m;

int n;

int blockSize[] = {100, 500, 200, 300, 600};

int processSize[] = {212, 417, 112, 426};

m = sizeof(blockSize) / sizeof(blockSize[0]);

n = sizeof(processSize) / sizeof(processSize[0]);

firstFit(blockSize, m, processSize, n);

return 0 ;

Output
Program to implement Best fit allocation method
#include<stdio.h>

#include<process.h>

main() {

int a[20], p[20], i, j, n, m;

printf("Enter no of Blocks.\n");

scanf("%d", &n);

for (i = 0; i < n; i++) {

printf("Enter the %dst Block size:", i);

scanf("%d", &a[i]);

printf("Enter no of Process.\n");

scanf("%d", &m);

for (i = 0; i < m; i++) {

printf("Enter the size of %dst Process:", i);

scanf("%d", &p[i]);

for (i = 0; i < n; i++) {

for (j = 0; j < m; j++) {

if (p[j] <= a[i]) {

printf("The Process %d allocated to %d\n", j, a[i]);

p[j] = 10000;

break;

for (j = 0; j < m; j++) {

if (p[j] != 10000) {
printf("The Process %d is not allocated\n", j);

Output
Program to implement Worst fit allocation method
#include <stdio.h>

void implementWorstFit(int blockSize[], int blocks, int processSize[], int processes) {

int allocation[processes];

int occupied[blocks];

for (int i = 0; i < processes; i++) {

allocation[i] = -1;

for (int i = 0; i < blocks; i++) {

occupied[i] = 0;

for (int i = 0; i < processes; i++) {

int indexPlaced = -1;

for (int j = 0; j < blocks; j++) {

if (blockSize[j] >= processSize[i] && !occupied[j]) {

if (indexPlaced == -1)

indexPlaced = j;

else if (blockSize[indexPlaced] < blockSize[j])

indexPlaced = j;

if (indexPlaced != -1) {

allocation[i] = indexPlaced;

occupied[indexPlaced] = 1;

blockSize[indexPlaced] -= processSize[i];

}
printf("\nProcess No.\tProcess Size\tBlock no.\n");

for (int i = 0; i < processes; i++) {

printf("%d \t\t\t %d \t\t\t", i + 1, processSize[i]);

if (allocation[i] != -1)

printf("%d\n", allocation[i] + 1);

else

printf("Not Allocated\n");

int main() {

int blockSize[] = {500, 400, 300, 200, 100};

int processSize[] = {100, 350, 400, 150, 200};

int blocks = sizeof(blockSize) / sizeof(blockSize[0]);

int processes = sizeof(processSize) / sizeof(processSize[0]);

implementWorstFit(blockSize, blocks, processSize, processes);

printf("\n");

return 0;

Output

You might also like