0% found this document useful (0 votes)
51 views7 pages

NP Seventh Week

The document contains code examples demonstrating the use of pipes and semaphores in C programs. It includes programs that open pipes to execute external commands and read/write their output/input. Other examples show using pipes between related processes like a producer and consumer. It also contains code for initializing, incrementing and decrementing a semaphore value using System V semaphore functions.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views7 pages

NP Seventh Week

The document contains code examples demonstrating the use of pipes and semaphores in C programs. It includes programs that open pipes to execute external commands and read/write their output/input. Other examples show using pipes between related processes like a producer and consumer. It also contains code for initializing, incrementing and decrementing a semaphore value using System V semaphore functions.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

/* Reading output from an external program trying to demonstrate popen "w" For theory do refer : page no 366 of beginning

to linux programming */ #include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<string.h> int main () { FILE *write_fp; char buffer[BUFSIZ + 1]; sprintf(buffer,"Once upon a time, there was...\n"); write_fp = popen("hostname", "w"); if (write_fp != NULL) { fwrite(buffer, sizeof(char), strlen(buffer), write_fp); pclose(write_fp); exit(EXIT_SUCCESS); } exit(EXIT_FAILURE); } =========== /* Reading output from an external program trying to demonstrate popen "r" For theory do refer : page no 366 of beginning to linux programming */ #include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<string.h> int main () { FILE *read_fp; char buffer[BUFSIZ + 1]; int chars_read; memset (buffer, '\0', sizeof (buffer)); read_fp = popen("uname -a","r"); if (read_fp != NULL) { chars_read = fread (buffer, sizeof (char), BUFSIZ, read_fp); if (chars_read > 0) { printf ("Output was:\n%s\n", buffer); } pclose (read_fp); exit (EXIT_SUCCESS); } exit (EXIT_FAILURE); } ===== /* Reading large amounts of data from pipe

For theory do refer : page no 366 of beginning to linux programming */ #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> int main() { FILE *read_fp; char buffer[BUFSIZ + 1]; int chars_read; memset(buffer, '\0', sizeof(buffer)); read_fp = popen("ps -ax", "r"); if (read_fp != NULL) { chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp); while (chars_read > 0) { buffer[chars_read - 1]= '\0'; printf("Reading:\n %s\n", buffer); chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp); } pclose(read_fp); exit(EXIT_SUCCESS); } exit(EXIT_FAILURE); } ======= /* Implementation of cat popen*.c | wc -l For theory do refer : page no 368 of beginning to linux programming */

#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> int main() { FILE *read_fp; char buffer[BUFSIZ + 1]; int chars_read; memset(buffer, '\0', sizeof(buffer)); read_fp = popen("cat popen*.c|wc -l ", "r"); if (read_fp != NULL) { chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp); while (chars_read > 0) { buffer[chars_read - 1] = '\0'; printf("Reading:\n %s\n", buffer); chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp); } pclose(read_fp); exit(EXIT_SUCCESS); } exit(EXIT_FAILURE); }

====== /* Writing to the pipe 3 bytes of data and reading from the pipe 3 bytes of data using pipe system call For theory do refer : page no 370 of beginning to linux programming */

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

======= /* pipes across fork For theory do refer : page no 371 of beginning to linux programming parent and child process. */ #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]; pid_t fork_result; memset(buffer,'\0',sizeof(buffer)); if (pipe(file_pipes) == 0) { fork_result = fork(); if (fork_result == -1) { fprintf(stderr, "Fork failure"); exit(EXIT_FAILURE); } if (fork_result == 0) { data_processed = read(file_pipes[0], buffer, BUFSIZ); printf("Read %d bytes: %s\n", data_processed, buffer); exit(EXIT_SUCCESS);

} else { data_processed = write(file_pipes[1], some_data, strlen(some_data)); printf("Wrote %d bytes\n", data_processed); } } exit(EXIT_SUCCESS); }

=====

/*The 'consumer' program, pipe4.c, that reads the data is much simpler:*/ #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { int data_processed; char buffer[BUFSIZ + 1]; int file_descriptor; memset(buffer, '\0', sizeof(buffer)); sscanf(argv[1], "%d", &file_descriptor); data_processed = read(file_descriptor, buffer, BUFSIZ); printf("%d read %d bytes: %s\n", getpid(), data_processed, buffer); exit(EXIT_SUCCESS); } ==== /* producer and consumer problem using pipes */ /* program of producer */ #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]; pid_t fork_result; memset(buffer, '\0', sizeof(buffer)); if (pipe(file_pipes) == 0) { fork_result = fork();

if (fork_result == (pid_t)-1) { fprintf(stderr, "Fork failure"); exit(EXIT_FAILURE); } if (fork_result == 0) { sprintf(buffer, "%d", file_pipes[0]); (void)execl("pipe4", "pipe4", buffer, (char *)0); exit(EXIT_FAILURE); } else { data_processed = write(file_pipes[1], some_data,strlen(some_data)); printf("%d wrote %d bytes\n", getpid(), data_processed); } } exit(EXIT_SUCCESS); }

====== /* semun.h header file decleration */ #ifndef _SEMUN_H #define _SEMUN_H union semun { int val; struct semid_ds *buf; unsigned short int *array; struct seminfo *__buf; }; #endif

/* /* /* /*

value for SETVAL */ buffer for IPC_STAT, IPC_SET */ array for GETALL, SETALL */ buffer for IPC_INFO */

======= #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #include "semun.h" static int set_semvalue(void); static void del_semvalue(void); static int semaphore_p(void); static int semaphore_v(void); static int sem_id; int main(int argc, char *argv[]) { int i; int pause_time; char op_char = 'O'; srand((unsigned int)getpid());

sem_id = semget((key_t)1234, 1, 0666 | IPC_CREAT); if (argc > 1) { if (!set_semvalue()) { fprintf(stderr, "Failed to initialize semaphore\n"); exit(EXIT_FAILURE); } op_char = 'X'; sleep(10); } for(i = 0; i < 10; i++) { if (!semaphore_p()) exit(EXIT_FAILURE); printf("%c", op_char);fflush(stdout); pause_time = rand() % 3; sleep(pause_time); printf("%c", op_char);fflush(stdout); if (!semaphore_v()) exit(EXIT_FAILURE); pause_time = rand() % 2; sleep(pause_time); } printf("\n%d finished\n", getpid()); if (argc > 1) { sleep(10); del_semvalue(); } exit(EXIT_SUCCESS); } static void del_semvalue(void) { union semun sem_union; if (semctl(sem_id, 0, IPC_RMID, sem_union) == -1) fprintf(stderr, "Failed to delete semaphore\n"); } static int set_semvalue(void) { union semun sem_union; sem_union.val = 1; if (semctl(sem_id, 0, SETVAL, sem_union) == -1) return(0); return(1); } static int semaphore_p(void) { struct sembuf sem_b; sem_b.sem_num = 0; sem_b.sem_op = -1; /* P() */ sem_b.sem_flg = SEM_UNDO; if (semop(sem_id, &sem_b, 1) == -1) { fprintf(stderr, "semaphore_p failed\n"); return(0); } return(1); } static int semaphore_v(void) { struct sembuf sem_b; sem_b.sem_num = 0; sem_b.sem_op = 1; /* V() */ sem_b.sem_flg = SEM_UNDO; if (semop(sem_id, &sem_b, 1) == -1) { fprintf(stderr, "semaphore_v failed\n"); return(0); }

return(1); } =====

You might also like