#include #include #include #include #include #include const char* share_file = "/design/tmp/dsmith/share.txt"; char linebuf[1024]; int buf_end_ptr; int curr_buf_ptr; int get_line(char *outstring, int FD); int main(int argc, char* argv[], char* envp[]) { char *prog_name = argv[0]; int i, status; int LOCKFD; flock_t f_struct; char input_line[80]; char temp_line[80]; char *user = getenv("USER"); char *hostname = getenv("HOSTNAME"); char *jobid = getenv("LSB_JOBID"); /* if (! -e $share_file) { if (system ("touch $share_file")) { die("can't create share file $share_file: $!"); } } */ for(i=1; i<=10; i++) { /* Open share file (in read + append) */ if((LOCKFD = open(share_file, O_RDWR | O_CREAT)) <= 0) { fprintf(stderr, "%s - can't open shared file %s\n", prog_name, share_file); exit(1); } f_struct.l_type = F_WRLCK; f_struct.l_whence = SEEK_SET; f_struct.l_start = 0; f_struct.l_len = 0; status = fcntl (LOCKFD, F_SETLK, &f_struct); if (status) { fprintf(stderr, "Another session of runtests is accessing the share file, waiting for if it to finish\n"); status = fcntl (LOCKFD, F_SETLKW, &f_struct); } if (status) { fprintf(stderr, "something bad with shared file\n"); exit(1); } buf_end_ptr = 0; curr_buf_ptr = 0; while(get_line(input_line, LOCKFD)) { if (strstr(input_line, hostname)) { fprintf (stdout, "Hello\n"); } } sprintf (temp_line, "hostname %s jobid %s attempt %d\n", hostname, jobid, i); write (LOCKFD, temp_line, strlen(temp_line)); f_struct.l_type = F_UNLCK; f_struct.l_whence = SEEK_SET; f_struct.l_start = 0; f_struct.l_len = 0; status = fcntl (LOCKFD, F_SETLK, &f_struct); if (status) { fprintf(stderr, "something bad with unlocking share file\n"); exit(1); } close(LOCKFD); } exit(0); } int get_line(char *outstring, int FD) { char *lineptr = outstring; int index = 0; int line_read = 0; int num_read; int eol_not_found = 1; if (buf_end_ptr == 0) { buf_end_ptr = read(FD, linebuf, 1024); curr_buf_ptr = 0; if (buf_end_ptr < 0) { fprintf(stderr, "something bad with reading file\n"); exit(1); } } while (eol_not_found) { if (curr_buf_ptr >= buf_end_ptr) { buf_end_ptr = read(FD, linebuf, 1024); curr_buf_ptr = 0; if (buf_end_ptr < 0) { fprintf(stderr, "something bad with reading file\n"); exit(1); } } if (buf_end_ptr == 0) { lineptr[0] = '\0'; return line_read; } line_read = 1; if ((*lineptr = linebuf[curr_buf_ptr++]) == '\n') { lineptr[1] = '\0'; return line_read; } lineptr++; } }