Difference between revisions of "Python"
From Tech
Jump to navigationJump to search (Created page with " <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…") |
|||
Line 78: | Line 78: | ||
Process(target=fight, args=(4,)).start() |
Process(target=fight, args=(4,)).start() |
||
+ | </nowiki> |
||
+ | |||
+ | |||
+ | <nowiki> |
||
+ | #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; |
||
+ | |||
+ | key=ftok("l-.l", 0x55); |
||
+ | printf("key=%i\n",key); |
||
+ | semid=semget(key, 1, IPC_CREAT|0666); |
||
+ | printf("semid=%i\n",semid); |
||
+ | release(semid); |
||
+ | printf("after first release"); |
||
+ | |||
+ | printf("semid=%i\n", semid); |
||
+ | if(fork()){ |
||
+ | p(semid, 1); |
||
+ | wait(&wait_status); |
||
+ | } else { |
||
+ | p(semid, 2); |
||
+ | } |
||
+ | |||
+ | } |
||
+ | |||
</nowiki> |
</nowiki> |
Revision as of 17:43, 8 February 2013
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()
#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; key=ftok("l-.l", 0x55); printf("key=%i\n",key); semid=semget(key, 1, IPC_CREAT|0666); printf("semid=%i\n",semid); release(semid); printf("after first release"); printf("semid=%i\n", semid); if(fork()){ p(semid, 1); wait(&wait_status); } else { p(semid, 2); } }