Search This Blog

Showing posts with label usage. Show all posts
Showing posts with label usage. Show all posts

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);
}

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);
}
}

Program to Understand Bitwise Operators

This is a C program that shows working of bitwise operators. The program takes two values, a and b. And displays the different bitwise operations' results. The program performs bitwise AND, bitwise OR, bitwise XOR, bitwise inversion (NOT), right bit shift and left bit shift operations.
#include<stdio.h>
main ()
{
unsigned int a = 60; /* 60 = 0011 1100*/
unsigned int b = 13; /* 13 = 0000 1101 */
printf ("a=60 ( 0011 1100)\nb=13 ( 0000 1101)\n"); 
int c = 0;
c = a&b ; /* 12 = 0000 1100 */
printf ("a&b=%d\n",c );
c = a | b ; /* 61 = 0011 1101 */
printf ("a|b=%d\n",c );
c = a ^ b ; /* 49 = 0011 0001 */
printf ("a^b=%d\n", c );
c = ~ a; /*-61 = 1100 0011 */
printf ("~a=%d\n" , c );
c = a << 2 ; /* 240 = 1111 0000 */
printf ("a<<2=%d\n" , c );
c = a >> 2 ; /*15 = 0000 1111*/
printf ("a>>2=%d\n" , c );
}