Difference between revisions of "Python"
From Tech
Jump to navigationJump to searchLine 1: | Line 1: | ||
− | <nowiki> |
||
− | import ctypes |
||
− | import os |
||
− | import sys |
||
− | |||
− | IPC_CREAT=00001000 |
||
− | # man shm_overview |
||
− | # for f in `find /lib/lib* -type f `; do nm -D $f|grep sem_open && echo found in library: $f; done |
||
− | locksUsed=0 |
||
− | class Lock: |
||
− | def __init__(self): |
||
− | #http://www.dailyfreecode.com/code/create-shared-memory-print-id-shared-2183.aspx |
||
− | global locksUsed |
||
− | self.fname="/tmp/Py-Lock%4i"%locksUsed |
||
− | |||
− | self.libc = ctypes.cdll.LoadLibrary("/lib/libc.so.6") |
||
− | self.librt = ctypes.cdll.LoadLibrary("/lib/librt.so.1") |
||
− | |||
− | f=open(self.fname,"w") |
||
− | f.write("0") |
||
− | f.close() |
||
− | self.key=self.libc.ftok(fname, 0x55) |
||
− | print "ftok returned:", self.key |
||
− | r=self.libc.semget(self.key, 1, IPC_CREAT|0666) |
||
− | print "semget returned: ",r |
||
− | |||
− | si=self.librt.sem_init(self.mem, 1, 1) |
||
− | print "sem_init returned:", si |
||
− | locksUsed+=1 |
||
− | |||
− | def acquire(self): |
||
− | self.librt.sem_wait(self.sm) |
||
− | |||
− | def release(self): |
||
− | self.librt.sem_post(self.sm) |
||
− | |||
− | #class Lock_POSIX: |
||
− | # def __init__(self): |
||
− | # global locksUsed |
||
− | # self.librt = ctypes.cdll.LoadLibrary("/lib/libpthread.so.0") |
||
− | # |
||
− | # try: |
||
− | # self.librt.sem_unlink("/PyLock%4i"%locksUsed) |
||
− | # except: |
||
− | # pass |
||
− | # self.sm=self.librt.sem_open("/PyLock%4i"%locksUsed, os.O_RDWR|os.O_CREAT, 0x1ff, 1) |
||
− | # if sm==0: |
||
− | # print "sem_open returned:", self.sm |
||
− | # sys.exit() |
||
− | # #si=self.librt.sem_init(self.sm, 1, 1) |
||
− | # print "sem_init returned:", si |
||
− | # if self.sm>=0: |
||
− | # locksUsed+=1 |
||
− | # |
||
− | # def acquire(self): |
||
− | # self.librt.sem_wait(self.sm) |
||
− | # |
||
− | # def release(self): |
||
− | # self.librt.sem_post(self.sm) |
||
− | |||
− | if __name__== "__main__": |
||
− | import time |
||
− | import random |
||
− | from multiprocessing import Process |
||
− | l=Lock() |
||
− | def fight(i): |
||
− | for j in range(10): |
||
− | l.acquire() |
||
− | for k in range(10): |
||
− | print i |
||
− | time.sleep(random.random()/30.) |
||
− | print |
||
− | l.release() |
||
− | |||
− | Process(target=fight, args=(1,)).start() |
||
− | Process(target=fight, args=(2,)).start() |
||
− | Process(target=fight, args=(3,)).start() |
||
− | Process(target=fight, args=(4,)).start() |
||
− | |||
− | </nowiki> |
||
− | |||
− | |||
<nowiki> |
<nowiki> |
||
#include <sys/types.h> |
#include <sys/types.h> |
||
Line 131: | Line 49: | ||
int semid; |
int semid; |
||
int wait_status; |
int wait_status; |
||
+ | union semun { |
||
+ | int val; /* Value for SETVAL */ |
||
+ | struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ |
||
+ | unsigned short *array; /* Array for GETALL, SETALL */ |
||
+ | struct seminfo *__buf; /* Buffer for IPC_INFO |
||
+ | (Linux-specific) */ |
||
+ | }; |
||
+ | int v; |
||
+ | union semun su; |
||
− | key=ftok("l-.l", 0x55); |
+ | //key=ftok("l-.l", 0x55); |
− | printf("key=%i\n",key); |
+ | //printf("key=%i\n",key); |
+ | key=IPC_PRIVATE; |
||
semid=semget(key, 1, IPC_CREAT|0666); |
semid=semget(key, 1, IPC_CREAT|0666); |
||
printf("semid=%i\n",semid); |
printf("semid=%i\n",semid); |
||
+ | su.val=1; |
||
− | release(semid); |
||
+ | v=semctl(semid, 0, SETVAL, su); |
||
− | printf("after first release"); |
||
+ | if(v==-1){ |
||
+ | perror("semctl0"); |
||
+ | exit(1); |
||
+ | } |
||
+ | |||
+ | v=semctl(semid, 0, GETVAL); |
||
+ | if(v==-1){ |
||
+ | perror("semctl1"); |
||
+ | exit(1); |
||
+ | } |
||
+ | printf("val after set: v=%i\n", v); |
||
− | printf("semid=%i\n", semid); |
||
if(fork()){ |
if(fork()){ |
||
p(semid, 1); |
p(semid, 1); |
Revision as of 18:11, 8 February 2013
#include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #include <stdlib.h> #include <stdio.h> int acquire(int semid){ struct sembuf sops[2]; sops[0].sem_num = 0; sops[0].sem_op = -1; sops[0].sem_flg = SEM_UNDO; if (semop(semid, sops, 1) == -1) { perror("acquire"); exit(1); } } int release(int semid){ struct sembuf sops[2]; sops[0].sem_num = 0; /* Operate on semaphore 0 */ sops[0].sem_op = 1; /* Wait for value to equal 0 */ sops[0].sem_flg = SEM_UNDO; if (semop(semid, sops, 1) == -1) { perror("release"); exit(1); } } void p(int semid, int i){ int j; acquire(semid); for(j=0; j<10; j++){ printf("%i\n",i); sleep(1); } release(semid); } int main(int argc, char **argv){ key_t key; int semid; int wait_status; union semun { int val; /* Value for SETVAL */ struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ unsigned short *array; /* Array for GETALL, SETALL */ struct seminfo *__buf; /* Buffer for IPC_INFO (Linux-specific) */ }; int v; union semun su; //key=ftok("l-.l", 0x55); //printf("key=%i\n",key); key=IPC_PRIVATE; semid=semget(key, 1, IPC_CREAT|0666); printf("semid=%i\n",semid); su.val=1; v=semctl(semid, 0, SETVAL, su); if(v==-1){ perror("semctl0"); exit(1); } v=semctl(semid, 0, GETVAL); if(v==-1){ perror("semctl1"); exit(1); } printf("val after set: v=%i\n", v); if(fork()){ p(semid, 1); wait(&wait_status); } else { p(semid, 2); } }