static gboolean publish_session_bus (const char *address) { HANDLE init_mutex; init_mutex = acquire_mutex (UNIQUE_DBUS_INIT_MUTEX); published_daemon_mutex = CreateMutexA (NULL, FALSE, DBUS_DAEMON_MUTEX); if (WaitForSingleObject (published_daemon_mutex, 10 ) != WAIT_OBJECT_0) { release_mutex (init_mutex); CloseHandle (published_daemon_mutex); published_daemon_mutex = NULL; return FALSE; } published_shared_mem = set_shm (DBUS_DAEMON_ADDRESS_INFO, address); if (!published_shared_mem) { release_mutex (init_mutex); CloseHandle (published_daemon_mutex); published_daemon_mutex = NULL; return FALSE; } release_mutex (init_mutex); return TRUE; }
int main() { mknod("sc_ctos", S_IFIFO|0666, 0); mknod("sc_svtoc", S_IFIFO|0666, 0); signal(SIGUSR1, sigHandler_add_serv); int ctos = open("sc_ctos", O_RDONLY); int svtoc = open("sc_svtoc", O_WRONLY); char buf[M]; char* avail; set_shm(); tS = 0; int pid, a; char e[MAX]; while(1) { read(ctos, buf, M); sscanf(buf, "%d %s", &pid, e); if(strcmp(e, "exit") == 0) { removeSvno(pid); continue; } sscanf(buf, "%d %d", &pid, &a); printf("%d %d\n", pid, a); avail = shmptr; int sn = getSvno(pid); if(sn >= 0) { write(stosv[sn], buf, M); } else { strcpy(buf, "No service available"); write(svtoc, buf, M); kill(pid, SIGUSR1); } } return 0; }
int main(int argc,char *argv[]) { int rate; if(argv[1] != NULL) rate = atoi(argv[1]); else rate = 3; buff_key = 101; buff_num = 3; cget_key = 103; cget_num = 1; shm_flg = IPC_CREAT | 0644; buff_ptr = (char *)set_shm(buff_key,buff_num,shm_flg); cget_ptr = (int *)set_shm(cget_key,cget_num,shm_flg); prod_key = 201; cmtx_key = 203; c_PG_key = 301; c_TP_key = 302; c_TG_key = 303; sem_flg = IPC_CREAT | 0644; sem_val = 1; prod_sem = set_sem(prod_key,sem_val,sem_flg); sem_val = 0; c_PG_sem = set_sem(c_PG_key,sem_val,sem_flg); c_TP_sem = set_sem(c_TP_key,sem_val,sem_flg); c_TG_sem = set_sem(c_TG_key,sem_val,sem_flg); sem_val = 1; cmtx_sem = set_sem(cmtx_key,sem_val,sem_flg); int pid1, pid2; pid1 = fork(); if(pid1 == 0) { while(1){ down(c_PG_sem); down(cmtx_sem); sleep(rate); printf("%dsmoker has tobacco T, receive paper %c and glue %c, smoking……\n", getpid(),buff_ptr[*cget_ptr + 1],buff_ptr[*cget_ptr + 2]); up(cmtx_sem); up(prod_sem); } } else { pid2 = fork(); if(pid2 == 0) { while(1){ down(c_TP_sem); down(cmtx_sem); sleep(rate); printf("%d smoker has glue G,receive paper %c and tobacco %c, smoking……\n", getpid(),buff_ptr[*cget_ptr + 1],buff_ptr[*cget_ptr]); up(cmtx_sem); up(prod_sem); } } else { while(1){ down(c_TG_sem); down(cmtx_sem); sleep(rate); printf("%d smoker has paper P, receive glue %c and tobacco %c, smoking…\n", getpid(),buff_ptr[*cget_ptr + 2],buff_ptr[*cget_ptr]); up(cmtx_sem); up(prod_sem); } } } return EXIT_SUCCESS; }
int main(){ int shmd, semd, fd, SHM_KEY, SEM_KEY; SHM_KEY = ftok("makefile", 1); SEM_KEY = ftok("makefile", 2); shmd = shmget(SHM_KEY, sizeof(int), 0644); semd = semget(SEM_KEY, 1, 0644); //frankly, I don't know if the semaphore is serving / could potentially serve any purpose based on the way I wrote this //really should get around to testing that if(!down(semd)){ //get size of last line int size = get_shm(shmd); char buffer[size+1]; printf("==================\n\n");// printf("size of buffer: %lu bytes\n\n",sizeof(buffer)); printf("buffer at creation: ");// print(buffer,size);// buffer[size] = '\0'; printf("buffer after terminating null added: ");// print(buffer,size);// //display the last line added to the file int i; fd = open("story.txt", O_RDWR); i = fd;// printf("open returns %d\n",i);// i = lseek(fd, (-1)*size, SEEK_END);// printf("lseek returns %d\n",i);// i = read(fd, buffer, size);//keep this one printf("read returns %d\n",i);// printf("buffer after attempting to read: ");// print(buffer,size);// if(i == -1){ printf("read error: %s\n", strerror(errno)); }else{ printf("==================\n");// printf("The story so far:\n"); printf("%s ...\n\n", buffer); //printf("Cannot figure out why the above won't print out as a char array. In any case, running \"./control --remove\" will correctly print out all the contents of the file. But this is kind of driving me crazy.\n\n"); //prompt the user to add another line (max 256 characters) printf("Add a new line (max 256 characters): "); char new_line[256]; fgets(new_line, sizeof(new_line), stdin); new_line[strlen(new_line) - 1] = ' '; new_line[strlen(new_line)] = '\0'; size = strlen(new_line); /* printf("new size (including space character): %d\n", size); */ //append the new line to the file lseek(fd, 0, SEEK_END); //shouldn't be necessary, but read() isn't working up above so this is necessary write(fd, new_line, size); } close(fd); //update shared memory set_shm(shmd, size); /* print_shm(shmd); */ //release the semaphore up(semd); return 0; }else{ printf("semaphore error: %s (make sure you've already run \"./control --create\")\n", strerror(errno)); return -1; } }