Lecture 09 Posixthreads
Lecture 09 Posixthreads
POSIX Threads
• Processes
– Programs running in their own address space
• Code
• Data
• Stack
• Threads
– Sections of code within a process that run independently
• Share address space
• Share global data
• Use of less resources
Program that makes an ordinary call to Program that creates a new thread to execute
processfd has a single thread of execution. processfd has two threads of execution.
• When a new thread is created it runs concurrently with the creating process.
• When creating a thread you indicate which function the thread should execute.
fd = *((int *)(arg));
for ( ; ; ) {
if ((nbytes = r_read(fd, buf, BUFSIZE)) <= 0)
break;
docommand(buf, nbytes);
}
return NULL;
}
#include <pthread.h>
pthread_t pthread_self(void);
• Since pthread_t may be a structure, use pthread_equal to
compare thread IDs for equality. The parameters of
pthread_equal are the thread IDs to be compared.
#include <pthread.h>
.
BIL 244 – System Programming
Exiting and cancellation
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#define PERMS (S_IRUSR | S_IWUSR)
#define READ_FLAGS O_RDONLY
#define WRITE_FLAGS (O_WRONLY | O_CREAT | O_TRUNC)
if (argc != 3) {
fprintf(stderr, "Usage: %s fromfile tofile\n", argv[0]);
return 1;
}
if (((fds[0] = open(argv[1], READ_FLAGS)) == -1) ||
((fds[1] = open(argv[2], WRITE_FLAGS, PERMS)) == -1)) {
perror("Failed to open the files");
return 1;
}
if (error = pthread_create(&tid, NULL, copyfilemalloc, fds)) {
fprintf(stderr, "Failed to create thread: %s\n", strerror(error));
return 1;
}
if (error = pthread_join(tid, (void **)&bytesptr)) {
fprintf(stderr, "Failed to join thread: %s\n", strerror(error));
return 1;
}
printf("Number of bytes copied: %d\n", *bytesptr);
return 0;
}
The copyfilemalloc function copies the contents of one file to another by calling the copyfile
function
#include <stdlib.h>
#include <unistd.h>
#include "restart.h"
if (error = pthread_attr_init(&tattr))
fprintf(stderr, "Failed to create attribute object: %s\n",
strerror(error));
else if (error = pthread_attr_setdetachstate(&tattr,
PTHREAD_CREATE_DETACHED))
fprintf(stderr, "Failed to set attribute state to detached: %s\n",
strerror(error));
else if (error = pthread_create(&tid, &tattr, processfd, &fd))
fprintf(stderr, "Failed to create thread: %s\n", strerror(error));
The thread stack
• You can set a location and size for the thread stack.
#include <pthread.h>
int error;
int fd;
pthread_attr_t tattr;
pthread_t tid;
if (error = pthread_attr_init(&tattr))
fprintf(stderr, "Failed to create an attribute object:%s\n",
strerror(error));
else if (error = pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM))
fprintf(stderr, "Failed to set scope to system:%s\n",
strerror(error));
else if (error = pthread_create(&tid, &tattr, processfd, &fd))
fprintf(stderr, "Failed to create a thread:%s\n", strerror(error));
Inheritance of scheduling policy: