SystemCall Programs
SystemCall Programs
SystemCall Programs
//Program 1
//Copy a file to another using system calls
//Usage of command line arguments
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
if (fd_one == -1)
{
printf("Error opening first_file\n");
close(fd_one);
return;
}
close(fd_one);
close(fd_two);}
//mv implemented using system call
//Program 2
#include<fcntl.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
int main(int argc, char **argv)
{
int fd1,fd2;
int n,count=0;
fd1=open(argv[1],O_RDONLY);
fd2=creat(argv[2],S_IWUSR);
rename(fd1,fd2);
unlink(argv[1]);
return (0);
}
//Fork and exec system call
//Program 3
char str[128];
strcpy(str, "Hello");
strcpy(str, "Howdy");
printf("I am the child and I changed str to %s. str's address is still %p\n", str, str); }
sleep(2);
printf("I am the parent. I just woke up. str's address is %p, and its value is %s\n", str, str);
return 0;
}
int main(int argc, char *argv[]) {
if ((children[i] = fork()) == 0)
exit(110 + i); }
assert(pid == children[i]);
return 0;
}
int main(int argc, char *argv[]) {
printf("Hello\n"); }
if (pidOrZero2 != 0) {
printf("Hi there\n");
return 0;