//C Program to hibernate Windows #includevoid main() { system("shutdown /h"); }
Search This Blog
Showing posts with label system call. Show all posts
Showing posts with label system call. Show all posts
C Program to Hibernate Windows
Do you want to hibernate your Windows computer from c? This is a c program to hibernate Windows operating System. If you execute this c program, it will hibernate your Windows computer instantly. The following is the c code to hibernate Windows:
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 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);
}
}
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)
