LAB PROGRAMS SITE

Unix Programs
Home | About Us | Notifications | Os & Sp Programs | UML Programs | Unix Programs | Cn Programs | Mm&Wd Programs | Useful Tips | Imp Sites | Contact Us

These Are Some Of The UNIX Programs

greenfly.gif

keyboard_dust_bunny_jumping_sm_wm.gif

PROGRAM FOR IPC

#include <unistd.h>

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

int main()

{

int data_processed;

int file_pipes[2];

const char some_data[] = "123";

char buffer[BUFSIZ + 1];

memset(buffer, '\0', sizeof(buffer));

if (pipe(file_pipes) == 0) {

data_processed = write(file_pipes[1], some_data, strlen(some_data));

printf("Wrote %d bytes\n", data_processed);

data_processed = read(file_pipes[0], buffer, BUFSIZ);

printf("Read %d bytes: %s\n", data_processed, buffer);

exit(EXIT_SUCCESS);

}

exit(EXIT_FAILURE);

}

laser_printing_job_sm_wm.gif

PROGRAM FOR LOCK2

#include <unistd.h>

#include <stdlib.h>

#include <stdio.h>

#include <fcntl.h>

#include <errno.h>

const char *lock_file = "/tmp/LCK.test2";

int main() {

int file_desc;

int tries = 10;

while (tries--) {

file_desc = open(lock_file, O_RDWR | O_CREAT | O_EXCL, 0444);

if (file_desc == -1) {

printf("%d - Lock already present\n", getpid());

sleep(3);

}

else {

/* critical region */

printf("%d - I have exclusive access\n", getpid());

sleep(1);

(void)close(file_desc);

(void)unlink(lock_file);

/* non-critical region */

sleep(2);

}

} /* while */

exit(EXIT_SUCCESS);

}

PROGRAM FOR LOCKS3

acowgirl.gif

#include <unistd.h>

#include <stdlib.h>

#include <stdio.h>

#include <fcntl.h>

const char *test_file = "/tmp/test_lock";

int main() {

int file_desc;

int byte_count;

char *byte_to_write = "A";

struct flock region_1;

struct flock region_2;

int res;

/* open a file descriptor */

file_desc = open(test_file, O_RDWR | O_CREAT, 0666);

if (!file_desc) {

fprintf(stderr, "Unable to open %s for read/write\n", test_file);

exit(EXIT_FAILURE);

}

/* put some data in the file */

for(byte_count = 0; byte_count < 100; byte_count++) {

(void)write(file_desc, byte_to_write, 1);

}

/* setup region 1, a shared lock, from bytes 10 -> 30 */

region_1.l_type = F_RDLCK;

region_1.l_whence = SEEK_SET;

region_1.l_start = 10;

region_1.l_len = 20;

/* setup region 2, an exclusive lock, from bytes 40 -> 50 */

region_2.l_type = F_WRLCK;

region_2.l_whence = SEEK_SET;

region_2.l_start = 40;

region_2.l_len = 10;

/* now lock the file */

printf("Process %d locking file\n", getpid());

res = fcntl(file_desc, F_SETLK, &region_1);

if (res == -1) fprintf(stderr, "Failed to lock region 1\n");

res = fcntl(file_desc, F_SETLK, &region_2);

if (res == -1) fprintf(stderr, "Failed to lock region 2\n");

/* and wait for a while */

sleep(60);

printf("Process %d closing file\n", getpid());

close(file_desc);

exit(EXIT_SUCCESS);

}

computer_to_computer_email_sm_wm.gif

PROGRAM FOR LOCK4

#include <unistd.h>

#include <stdlib.h>

#include <stdio.h>

#include <fcntl.h>

 

const char *test_file = "/tmp/test_lock";

#define SIZE_TO_TRY 5

void show_lock_info(struct flock *to_show);

 

int main() {

int file_desc;

int res;

struct flock region_to_test;

int start_byte;

/* open a file descriptor */

file_desc = open(test_file, O_RDWR | O_CREAT, 0666);

if (!file_desc) {

fprintf(stderr, "Unable to open %s for read/write", test_file);

exit(EXIT_FAILURE);

}

for (start_byte = 0; start_byte < 99; start_byte += SIZE_TO_TRY) {

/* set up the region we wish to test */

region_to_test.l_type = F_WRLCK;

region_to_test.l_whence = SEEK_SET;

region_to_test.l_start = start_byte;

region_to_test.l_len = SIZE_TO_TRY;

region_to_test.l_pid = -1;

printf("Testing F_WRLCK on region from %d to %d\n",

start_byte, start_byte + SIZE_TO_TRY);

/* now test the lock on the file */

res = fcntl(file_desc, F_GETLK, &region_to_test);

if (res == -1) {

fprintf(stderr, "F_GETLK failed\n");

exit(EXIT_FAILURE);

}

if (region_to_test.l_pid != -1) {

printf("Lock would fail. F_GETLK returned:\n");

show_lock_info(&region_to_test);

}

else {

printf("F_WRLCK - Lock would succeed\n");

}

/* now repeat the test with a shared (read) lock */

/* set up the region we wish to test */

region_to_test.l_type = F_RDLCK;

region_to_test.l_whence = SEEK_SET;

region_to_test.l_start = start_byte;

region_to_test.l_len = SIZE_TO_TRY;

region_to_test.l_pid = -1;

printf("Testing F_RDLCK on region from %d to %d\n",

start_byte, start_byte + SIZE_TO_TRY);

/* now test the lock on the file */

res = fcntl(file_desc, F_GETLK, &region_to_test);

if (res == -1) {

fprintf(stderr, "F_GETLK failed\n");

exit(EXIT_FAILURE);

}

if (region_to_test.l_pid != -1) {

printf("Lock would fail. F_GETLK returned:\n");

show_lock_info(&region_to_test);

}

else {

printf("F_RDLCK - Lock would succeed\n");

}

} /* for */

close(file_desc);

exit(EXIT_SUCCESS);

}

void show_lock_info(struct flock *to_show) {

printf("\tl_type %d, ", to_show->l_type);

printf("l_whence %d, ", to_show->l_whence);

printf("l_start %d, ", (int)to_show->l_start);

printf("l_len %d, ", (int)to_show->l_len);

printf("l_pid %d\n", to_show->l_pid);

}

 

man_stuck_in_comp.gif

PROGRAM FOR GREP

#!/bin/sh

#first

#this file looks through all the files in the current directory for string POSIX.

for file in *

do

if grep -q POSIX $file

then

echo $file

fi

done

exit 0

worm_chomping_on_computer_sm_wm.gif

PROGRAM FOR SIGNALS AND PROCESS

/* We'll start by writing the function which reacts to the signal

which is passed in the parameter sig.

This is the function we will arrange to be called when a signal occurs.

We print a message, then reset the signal handling for SIGINT

(by default generated by pressing CTRL-C) back to the default behavior.

Let's call this function ouch. */

#include <signal.h>

#include <stdio.h>

#include <unistd.h>

void ouch(int sig)

{

printf("OUCH! - I got signal %d\n", sig);

(void) signal(SIGINT, SIG_DFL);

}

/* The main function has to intercept the SIGINT signal generated when we type Ctrl-C .

For the rest of the time, it just sits in an infinite loop,

printing a message once a second. */

int main()

{

(void) signal(SIGINT, ouch);

while(1) {

printf("Hello World!\n");

sleep(1);

}

}

india_fl_md_wht.gif

UDAY TECHNOLOGIES