criu/test/testee-threads.c
Cyrill Gorcunov 9fcecfe1a2 test: Make testee-threads to create thread inside thread
And open a file as well

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2011-10-23 12:43:15 +04:00

103 lines
1.8 KiB
C

/*
* A simple testee program with threads
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <pthread.h>
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static int counter;
static void *ff1(void *arg)
{
void *map_unreadable = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
(void)map_unreadable;
while (1) {
pthread_mutex_lock(&mtx);
counter++;
printf("%d: Counter value: %d\n", getpid(), counter);
pthread_mutex_unlock(&mtx);
sleep(5);
}
return NULL;
}
static void *f1(void *arg)
{
const char name[] = "f1-file";
pthread_t th;
int fd;
void *map_unreadable = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
(void)map_unreadable;
unlink(name);
fd = open(name, O_CREAT, 0644);
if (fd >= 0)
write(fd, name, sizeof(name));
if (pthread_create(&th, NULL, &ff1, NULL))
perror("Cant create thread");
while (1) {
pthread_mutex_lock(&mtx);
counter++;
printf("%d: Counter value: %d\n", getpid(), counter);
pthread_mutex_unlock(&mtx);
sleep(2);
}
return NULL;
}
static void *f2(void *arg)
{
void *map_unreadable = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
(void)map_unreadable;
while (1) {
pthread_mutex_lock(&mtx);
counter--;
printf("%d: Counter value: %d\n", getpid(), counter);
pthread_mutex_unlock(&mtx);
sleep(3);
}
return NULL;
}
int main(int argc, char *argv[])
{
pthread_t th1, th2;
int rc1, rc2;
printf("%s pid %d\n", argv[0], getpid());
rc1 = pthread_create(&th1, NULL, &f1, NULL);
rc2 = pthread_create(&th2, NULL, &f2, NULL);
if (rc1 | rc2)
exit(1);
pthread_join(th1, NULL);
pthread_join(th2, NULL);
exit(0);
}