Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <pthread.h>
- #include <mqueue.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <signal.h>
- #include <memory.h>
- #include <asm/errno.h>
- #include <errno.h>
- #include <sys/stat.h>
- static void tfunc(union sigval sv)
- {
- mqd_t mqdes = *((mqd_t *) sv.sival_ptr);
- struct sigevent sev;
- sev.sigev_notify = SIGEV_THREAD;
- sev.sigev_notify_function = tfunc;
- sev.sigev_notify_attributes = NULL;
- sev.sigev_value.sival_ptr = sv.sival_ptr;
- if (mq_notify(mqdes, &sev) == -1){
- perror("Napaka pri registraciji obvestil ");
- exit(EXIT_FAILURE);
- }
- struct mq_attr attr;
- if (mq_getattr(mqdes, &attr) == -1){
- perror("Napaka pri pridobivanju atributov ");
- exit(EXIT_FAILURE);
- }
- char* msg = malloc(attr.mq_msgsize + 1);
- if (msg == NULL){
- perror("Napaka pri rezerviranju pomnilnika ");
- exit(EXIT_FAILURE);
- }
- while(1) {
- memset(msg, 0, attr.mq_msgsize + 1);
- ssize_t nr = mq_receive(mqdes, msg, attr.mq_msgsize, NULL);
- if (nr == -1) {
- if(errno == EAGAIN){
- break;
- }else{
- perror("Napaka pri pridobivanju sporocila ");
- exit(EXIT_FAILURE);
- }
- }
- printf("Vrsta: %s\n", msg);
- }
- free(msg);
- }
- volatile int stop = 0;
- void signal_handler(int sig)
- {
- if(sig == SIGINT)
- {
- stop = 1;
- }
- }
- void zapri_cev(int fd, char* ime){
- close(fd);
- unlink(ime);
- }
- mqd_t zapri_vrsto(int mq, char* ime){
- mq_close(mq);
- mq_unlink(ime);
- }
- int main(int argc, char *argv[]) {
- mqd_t mqdes;
- if (argc != 3) {
- fprintf(stderr, "Usage: %s <ime_cevi> <ime_vrste>\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- char *ime_cevi = argv[1], *ime_vrste = argv[2];
- printf("Ime cevi: %s, ime vrste: %s\n", ime_cevi, ime_vrste);
- mqdes = mq_open(ime_vrste, O_RDONLY|O_CREAT|O_NONBLOCK, 0666);
- if (mqdes == (mqd_t) -1){
- perror("Napaka pri ustvarjanju cevi ");
- return EXIT_FAILURE;
- }
- if(mkfifo(ime_cevi, 0666) == -1){
- perror("Napaka pri ustvarjanju cevi ");
- zapri_vrsto(mqdes, ime_vrste);
- return EXIT_FAILURE;
- }
- int vh_cev = open(ime_cevi, O_RDONLY|O_NONBLOCK);
- if(vh_cev == -1)
- {
- perror("Napaka pri odpiranju cevi ");
- zapri_vrsto(mqdes, ime_vrste);
- unlink(ime_cevi);
- return EXIT_FAILURE;
- }
- if (fcntl(vh_cev,F_SETFL,O_ASYNC|O_NONBLOCK) == -1) {
- perror("Napaka v fcntl ");
- zapri_vrsto(mqdes, ime_vrste);
- zapri_cev(vh_cev, ime_cevi);
- return EXIT_FAILURE;
- }
- if (fcntl(vh_cev,F_SETOWN,getpid()) == -1){
- perror("Napaka v fcntl ");
- zapri_vrsto(mqdes, ime_vrste);
- zapri_cev(vh_cev, ime_cevi);
- return EXIT_FAILURE;
- }
- struct sigaction sa;
- memset(&sa,0,sizeof(sigaction));
- sa.sa_handler = signal_handler;
- sigaction(SIGINT, &sa, 0);
- sigaction(SIGIO, &sa, 0);
- struct sigevent sev;
- sev.sigev_notify = SIGEV_THREAD;
- sev.sigev_notify_function = tfunc;
- sev.sigev_notify_attributes = NULL;
- sev.sigev_value.sival_ptr = &mqdes;
- if (mq_notify(mqdes, &sev) == -1){
- perror("Napaka pri registraciji obvestil ");
- zapri_vrsto(mqdes, ime_vrste);
- zapri_cev(vh_cev, ime_cevi);
- return EXIT_FAILURE;
- }
- int l;
- char buffer[1024];
- while(1){
- pause();
- if(stop == 1){
- zapri_cev(vh_cev, ime_cevi);
- zapri_vrsto(mqdes, ime_vrste);
- exit(EXIT_SUCCESS);
- }
- int first = 1;
- while(1){
- while(1)
- {
- l = read(vh_cev, buffer, sizeof(buffer));
- if(l>0)
- {
- if(first){
- printf("Cev: ");
- fflush(stdout);
- first = 0;
- }
- write(1,buffer,l);
- }else{
- break;
- }
- }
- if(l < 0)
- {
- if(errno != EAGAIN && errno != EWOULDBLOCK)
- {
- perror("Tezava pri branju iz cevi ");
- zapri_cev(vh_cev, ime_cevi);
- zapri_vrsto(mqdes, ime_vrste);
- exit(EXIT_FAILURE);
- }
- }else{
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement