You can schedule shutdown in Windows or Linux using C program. We will see how to schedule a shutdown using a c program. To schedule a shutdown in Windows, we execute the Windows command shutdown /s /t sec (where sec is time delay before shutdown in seconds). In Linux, we execute the command shutdown -h +600. The c programs to schedule a shutdown in Windows and Linux are given separately.
Search This Blog
Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts
C Program to Shutdown Computer - How to Shutdown Computer Using C Code
Computer can be shutdown using c program. But the commands are different in Windows and Linux. The system() function in c can be used to execute system commands ( Windows command or Linux command ). To shutdown the computer using c program, we execute the appropriate shutdown command using the system() function.
C Program to Simulate the ls Command in Linux Operating system
This is a c program to do Simulation of ls Command in linux. The ls command lists all the contents of the directory including filse and sub-directories. The following c program in simulates the ls command.
C Program to Simulate Round Robin CPU Scheduling Algorithm
C Program to Simulate Priority CPU Scheduling Algorithm
C Program to Simulate First Come First Serve (FCFS) CPU Scheduling Algorithm
C Program to Simulate rmdir Command or Delete Directory
C Program to Open, Read and Write Files
C Program to Create Process and Display Process ID - OS Lab Program
How to Use Fork and Exec System Calls
How to Use Exit System Call
C Program to make Parent Process Wait for Child to Terminate
C Program to Simulate GREP Command in Linux
C Program to make Child Process an Orphan Process
C Program to Show Process ID in Linux
C Program for Inter Process Communication using Pipes or Named Pipes (Chat Program)
C Program to Create a Process Using Fork System Call
#include<stdio.h>
#include<dirent.h>
main()
{
char dirname[10];
DIR*p;
struct dirent *d;
printf("Enter directory name\n");
scanf("%s",dirname);
p=opendir(dirname);
if(p==NULL)
{
perror("Cannot find directory");
exit(-1);
}
while(d=readdir(p))
printf("%s\n",d->d_name);
}
Related Posts:
C Program to Simulate ls Command in LinuxC Program to Simulate Round Robin CPU Scheduling Algorithm
C Program to Simulate Priority CPU Scheduling Algorithm
C Program to Simulate First Come First Serve (FCFS) CPU Scheduling Algorithm
C Program to Simulate rmdir Command or Delete Directory
C Program to Open, Read and Write Files
C Program to Create Process and Display Process ID - OS Lab Program
How to Use Fork and Exec System Calls
How to Use Exit System Call
C Program to make Parent Process Wait for Child to Terminate
C Program to Simulate GREP Command in Linux
C Program to make Child Process an Orphan Process
C Program to Show Process ID in Linux
C Program for Inter Process Communication using Pipes or Named Pipes (Chat Program)
C Program to Create a Process Using Fork System Call
C Program to Simulate grep Command in Linux
This post contains C program to simulate grep command in Linux or Unix operating systems
Simulation of grep command in Linux
C Program to Simulate Round Robin CPU Scheduling Algorithm
C Program to Simulate Priority CPU Scheduling Algorithm
C Program to Simulate Shortest Job First (SJF) CPU Scheduling Algorithm
C Program to Simulate First Come First Serve (FCFS) CPU Scheduling Algorithm
C Program to Simulate rmdir Command or Delete Directory
C Program to Open, Read and Write Files
C Program to Create Process and Display Process ID - OS Lab Program
How to Use Fork and Exec System Calls
How to Use Exit System Call
C Program to make Parent Process Wait for Child to Terminate
C Program to make Child Process an Orphan Process
C Program to Show Process ID in Linux
C Program for Inter Process Communication using Pipes or Named Pipes (Chat Program)
C Program to Create a Process Using Fork System Call
Simulation of grep command in Linux
#include<stdio.h>
#include<string.h>
void main()
{
char fn[10],pat[10],temp[200];
FILE *fp;
printf("Enter file name\n");
scanf("%s",fn);
printf("Enter pattern to be searched\n");
scanf("%s",pat);
fp=fopen(fn,"r");
while(!feof(fp))
{
fgets(temp,1000,fp);
if(strstr(temp,pat))
printf("%s",temp);
}
fclose(fp);
}
Related Posts:
C Program to Simulate ls Command in LinuxC Program to Simulate Round Robin CPU Scheduling Algorithm
C Program to Simulate Priority CPU Scheduling Algorithm
C Program to Simulate Shortest Job First (SJF) CPU Scheduling Algorithm
C Program to Simulate First Come First Serve (FCFS) CPU Scheduling Algorithm
C Program to Simulate rmdir Command or Delete Directory
C Program to Open, Read and Write Files
C Program to Create Process and Display Process ID - OS Lab Program
How to Use Fork and Exec System Calls
How to Use Exit System Call
C Program to make Parent Process Wait for Child to Terminate
C Program to make Child Process an Orphan Process
C Program to Show Process ID in Linux
C Program for Inter Process Communication using Pipes or Named Pipes (Chat Program)
C Program to Create a Process Using Fork System Call
C Program to Remove (Delete) a Directory using LINUX System Calls
This is a C program to delete or remove a directory using the system calls in Linux or Unix operating systems. The program make use of the remove() system call in Linux.
#include<stdio.h>
#include<fcntl.h>
main()
{
char fn[10];
printf("Enter source filename\n");
scanf("%s",fn);
if(remove(fn)==0)
printf("File removed\n");
else
printf("File cannot be removed\n");
}
#include<stdio.h>
#include<fcntl.h>
main()
{
char fn[10];
printf("Enter source filename\n");
scanf("%s",fn);
if(remove(fn)==0)
printf("File removed\n");
else
printf("File cannot be removed\n");
}
C Program to open, read and write files and perform file copy operation usingSystem Calls in UNIX
Write a program to open, read and write files and perform file copy operation. This program show how to open, read, write and copy files using Linux or Unix system calls like open(), read() and write().
#include<stdio.h>
#include<fcntl.h>
main()
{
char buff[1000],fn1[10],fn2[10];
int fd1,fd2,n;
printf("Enter source filename\n");
scanf("%s",fn1);
printf("Enter destination file name\n");
scanf("%s",fn2);
fd1=open(fn1,O_RDONLY);
n=read(fd1,buff,1000);
fd2=open(fn2,O_CREAT|O_WRONLY);
n=write(fd2,buff,n);
close(fd1);
close(fd2);
}
#include<stdio.h>
#include<fcntl.h>
main()
{
char buff[1000],fn1[10],fn2[10];
int fd1,fd2,n;
printf("Enter source filename\n");
scanf("%s",fn1);
printf("Enter destination file name\n");
scanf("%s",fn2);
fd1=open(fn1,O_RDONLY);
n=read(fd1,buff,1000);
fd2=open(fn2,O_CREAT|O_WRONLY);
n=write(fd2,buff,n);
close(fd1);
close(fd2);
}
C Program to Open, Read and Write a file using System Calls
This is a C program to open , read and write files using system calls in Linux (UNIX) operating systems. The system calls open(), read() and write() are used in the C program to open, read and write files in Unix/Linux operating systems.
#include<stdio.h>
#include<fcntl.h>
main()
{
char buff[100],fn[10];
int fd,n;
printf("Enter file name\n");
scanf("%s",fn);
fd=open(fn,O_RDONLY);
n=read(fd,buff,100);
n=write(1,buff,n);
close(fd);
}
#include<stdio.h>
#include<fcntl.h>
main()
{
char buff[100],fn[10];
int fd,n;
printf("Enter file name\n");
scanf("%s",fn);
fd=open(fn,O_RDONLY);
n=read(fd,buff,100);
n=write(1,buff,n);
close(fd);
}
Using System calls in C Program to rename a Directory in Linux
C Program to rename a directory in Unix or Linux operating systems using system call rename().
#include<stdio.h>
main()
{
char s[10],d[10];
printf("Enter source Dir Name:\n");
scanf("%s",s);
printf("Enter New Dir Name:\n");
scanf("%s",d);
if(rename(s,d)==-1)
perror("Can't be changed\n");
else
printf("%s is changed to %s\n\n",s,d);
}
#include<stdio.h>
main()
{
char s[10],d[10];
printf("Enter source Dir Name:\n");
scanf("%s",s);
printf("Enter New Dir Name:\n");
scanf("%s",d);
if(rename(s,d)==-1)
perror("Can't be changed\n");
else
printf("%s is changed to %s\n\n",s,d);
}
How to Create process and display Process ID (pid) of Both Parent and Child
This program shows how to create process and display Process ID (pid) of Both Parent and Child processes. The process is created using the fork system call in UNIX (Linux) operating systems.
#include<stdio.h>
#include<dirent.h>
main(int argc,char **argv)
{
int pid,i;
for(i=0;i<atoi(argv[1]);i++)
{
pid=fork();
if(pid==0)
{
printf("child process id %d Parent process
id %d\n",getpid(),getppid());
}}}
#include<stdio.h>
#include<dirent.h>
main(int argc,char **argv)
{
int pid,i;
for(i=0;i<atoi(argv[1]);i++)
{
pid=fork();
if(pid==0)
{
printf("child process id %d Parent process
id %d\n",getpid(),getppid());
}}}
How to use fork and exec System Call
Using fork and exec System calls in Linux (UNIX) operating systems. Fork system call is used to create a child process from a parent process.
#include<stdio.h>
main()
{
int pid;
pid=fork();
printf("%d\n",pid);
if(pid==0)
{
exec("/bin/date\n",NULL,NULL);
exit(0); }
else
{
printf("Parent process %d\n",pid); }}
if(sp)
while(dd=readdir(sp))
printf("--> %s\n",dd->d_name);
}
}
#include<stdio.h>
main()
{
int pid;
pid=fork();
printf("%d\n",pid);
if(pid==0)
{
exec("/bin/date\n",NULL,NULL);
exit(0); }
else
{
printf("Parent process %d\n",pid); }}
if(sp)
while(dd=readdir(sp))
printf("--> %s\n",dd->d_name);
}
}
How to use exit() System call
C program to show how to use exit system call. The function exit() is used to exit from a process (or to terminate a process). This works both in Linux or UNIX and Windows operating systems.
#include<stdio.h>
main()
{
int pid;
pid=fork();
printf("%d\n",pid);
if(pid<0)
{
perror("Child can't be executed\n");
exit(-1);
}
else
{
printf("Child created\n");
exit(0);
}
}
#include<stdio.h>
main()
{
int pid;
pid=fork();
printf("%d\n",pid);
if(pid<0)
{
perror("Child can't be executed\n");
exit(-1);
}
else
{
printf("Child created\n");
exit(0);
}
}
How to make parent process wait till Completion of Child Process (Joining parent with child process)
How to make the parent process wait till the completion of execution of child process. The following C program makes the parent process to wait till the completion of its child process.
#include<stdio.h>
main()
{
int pid;
pid=fork();
printf("%d\n",pid);
if(pid==0)
{
printf("From child process \n");
}
else
{
wait(0);
printf("From parent process\n");
}
}
#include<stdio.h>
main()
{
int pid;
pid=fork();
printf("%d\n",pid);
if(pid==0)
{
printf("From child process \n");
}
else
{
wait(0);
printf("From parent process\n");
}
}
How to make Child process an Orphan Process
Inter Process Communication using Named Pipes - Chat Program in C
In this post, we will see a chat program using named pipes (fifo). Pipes are used for communication between processes. The named pipes are fifos. They enable two way communication unlike ordinary pipes. But they are half duplex, i.e. communication can take place only in one direction at a time. The program is in 3 parts: pipe_creation.c, leftTerminal.c and rightTerminal.c.
pipe_creation.c
rightTerminal.c
How to run:
Run pipe_creation.c first. Then close it. Then run leftTerm.c. Without closing it, open a new terminal window and run rightTerm.c. Start typing from leftTerm.c and then in rightTerm.c.
Output :
pipe_creation.c:
pipeA created
pipeB is created
leftTerminal.c
Enter text:
hi
Hai
Enter text:
fine?
Ya.Sure
Enter text:
end
rightTerminal.c
hi
Enter text:
Hai
fine?
Enter text:
Ya.Sure
end
Enter text:
end
C Program to Simulate Round Robin CPU Scheduling Algorithm
C Program to Simulate Priority CPU Scheduling Algorithm
C Program to Simulate First Come First Serve (FCFS) CPU Scheduling Algorithm
C Program to Simulate rmdir Command or Delete Directory
C Program to Open, Read and Write Files
C Program to Create Process and Display Process ID - OS Lab Program
How to Use Fork and Exec System Calls
How to Use Exit System Call
C Program to make Parent Process Wait for Child to Terminate
C Program to Simulate GREP Command in Linux
C Program to make Child Process an Orphan Process
C Program to Show Process ID in Linux
C Program to Create a Process Using Fork System Call
pipe_creation.c
#include<stdio.h>
void main()
{
int f1,f2;
f1 = mkfifo("pipeA",0666);
if(f1<0)
printf("\npipeA was not created");
else
printf("\npipeA created");
f2 = mkfifo("pipeB",0666);
if(f2<0)
printf("\npipeB was not created");
else
printf("\npipeB is created\n");
}
leftTerminal.c
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char str[256]="start";
int fifo_write,fifo_read;
while(strcmp(str,"end")!=0)
{
fifo_write= open("pipeA",O_WRONLY);
if(fifo_write<0)
printf("\nError opening pipe");
else
{
printf("\nEnter text:\n");
scanf("%s",str);
write(fifo_write,str,255*sizeof(char));
close(fifo_write);
}
fifo_read=open("pipeB",O_RDONLY);
if(fifo_read<0)
printf("\nError opening write pipe");
else
{
read(fifo_read,str,255*sizeof(char));
close(fifo_read);
printf("\n%s",str);
}
}
rightTerminal.c
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char str[256]="start";
int fifo_read,fifo_write;
while(strcmp(str,"end")!=0)
{
fifo_read=open("pipeA",O_RDONLY);
if(fifo_read<0)
printf("\nError opening read pipe");
else
{
read(fifo_read,str,255*sizeof(char));
close(fifo_read);
printf("\n%s",str);
}
fifo_write=open("pipeB",O_WRONLY);
if(fifo_write<0)
printf("\nError opening write pipe");
else
{
printf("\nEnter text:\n");
scanf("%s",str);
write(fifo_write,str,255*sizeof(char));
close(fifo_write);
}
}
}
Run pipe_creation.c first. Then close it. Then run leftTerm.c. Without closing it, open a new terminal window and run rightTerm.c. Start typing from leftTerm.c and then in rightTerm.c.
Output :
pipe_creation.c:
pipeA created
pipeB is created
leftTerminal.c
Enter text:
hi
Hai
Enter text:
fine?
Ya.Sure
Enter text:
end
rightTerminal.c
hi
Enter text:
Hai
fine?
Enter text:
Ya.Sure
end
Enter text:
end
Related Posts:
C Program to Simulate ls Command in LinuxC Program to Simulate Round Robin CPU Scheduling Algorithm
C Program to Simulate Priority CPU Scheduling Algorithm
C Program to Simulate First Come First Serve (FCFS) CPU Scheduling Algorithm
C Program to Simulate rmdir Command or Delete Directory
C Program to Open, Read and Write Files
C Program to Create Process and Display Process ID - OS Lab Program
How to Use Fork and Exec System Calls
How to Use Exit System Call
C Program to make Parent Process Wait for Child to Terminate
C Program to Simulate GREP Command in Linux
C Program to make Child Process an Orphan Process
C Program to Show Process ID in Linux
C Program to Create a Process Using Fork System Call
Creating A Process in Linux (UNIX) - fork() Example Program
The system call fork() is used to create a new process in UNIX based operating systems and Linux systems. The fork() system call creates a child process when called from a parent process. Unix will make an exact copy of the parent's address space and give it to the child. Therefore, the parent and child processes have separate address spaces. Here is a C program which uses the fork() system call to create a process during execution. The program is commented well for better understanding.
Program
/*The program (parent process) reads an array from the user.
It sorts it in descending order.
The child process created by parent process sorts it ascending order
*/
#include<stdlib.h>
#include<stdio.h>
int main()
{
int cpid;
int temp,a[100],n,i,j;
printf("\nEnter number of elements : ");
scanf("%d",&n);
printf("Enter elements\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("\n");
cpid=fork(); /*Creating child process using fork() system call */
if(cpid<0)
{
printf("Cannot create a new process\n");
exit(0);
}
if(cpid != 0)
{
/*fork returns processid of newly created process.
Therefore, the value of integer variable cpid in parent process is the process id of child process.
This block (if block) is executed only by the parent because the value of cpid is zero in child process. We use the value of variable cpid to decide which process (parent or child) should execute which part of code*/
printf("\nThis is parent with id: %d\n",getpid());
/* The UNIX system call getpid() return the process id of process being executed.*/
for(i=0;i<n;i++){
for(j=0;j<n-1;j++){
if(a[j]<a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Parent sorted the array in descending order\n");
}else
{
/*This block (else block) is executed only by the child process because value of cpid is zero only for child process.*/
printf("\nThis is child with id: %d \n",getpid());
for(i=0;i<n;i++){
for(j=0;j<n-1;j++){
if(a[j]>a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Child sorted the array in ascending order\n");
}
/*The following part of code is executed by both the processes because it is not restricted using the value of cpid.*/
for(i=0;i<n;i++){
printf("%d\n",a[i]);
}
return 0;
}
Output:
Enter number of elements : 5
Enter elements
8
6
10
2
12
This is parent with id: 4261
Parent sorted the array in descending order
12
10
8
6
2
This is child with id: 4262
Child sorted the array in ascending order
2
6
8
10
12
Program
/*The program (parent process) reads an array from the user.
It sorts it in descending order.
The child process created by parent process sorts it ascending order
*/
#include<stdlib.h>
#include<stdio.h>
int main()
{
int cpid;
int temp,a[100],n,i,j;
printf("\nEnter number of elements : ");
scanf("%d",&n);
printf("Enter elements\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("\n");
cpid=fork(); /*Creating child process using fork() system call */
if(cpid<0)
{
printf("Cannot create a new process\n");
exit(0);
}
if(cpid != 0)
{
/*fork returns processid of newly created process.
Therefore, the value of integer variable cpid in parent process is the process id of child process.
This block (if block) is executed only by the parent because the value of cpid is zero in child process. We use the value of variable cpid to decide which process (parent or child) should execute which part of code*/
printf("\nThis is parent with id: %d\n",getpid());
/* The UNIX system call getpid() return the process id of process being executed.*/
for(i=0;i<n;i++){
for(j=0;j<n-1;j++){
if(a[j]<a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Parent sorted the array in descending order\n");
}else
{
/*This block (else block) is executed only by the child process because value of cpid is zero only for child process.*/
printf("\nThis is child with id: %d \n",getpid());
for(i=0;i<n;i++){
for(j=0;j<n-1;j++){
if(a[j]>a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Child sorted the array in ascending order\n");
}
/*The following part of code is executed by both the processes because it is not restricted using the value of cpid.*/
for(i=0;i<n;i++){
printf("%d\n",a[i]);
}
return 0;
}
Output:
Enter number of elements : 5
Enter elements
8
6
10
2
12
This is parent with id: 4261
Parent sorted the array in descending order
12
10
8
6
2
This is child with id: 4262
Child sorted the array in ascending order
2
6
8
10
12
![]() |
| Output of the program |
Subscribe to:
Posts (Atom)
