GDBMS 1.0
|
00001 #include "maininclude.h" 00002 #include "listi.h" 00003 #include "settings.h" 00004 #include "hash_table.h" 00005 #include "tr_twin_ht.h" 00006 #include "file_semaphore.h" 00007 #include "some_functions.h" 00008 #include "initial_mode_commands.h" 00009 #include "session_mode_commands.h" 00010 #include "transaction_mode_commands.h" 00011 #include "transaction_mode_commands_types.h" 00012 #include "transaction_mode_commands_nodes.h" 00013 #include "transaction_mode_commands_edges.h" 00014 #include "transaction_mode_commands_find.h" 00015 #include <sys/types.h> 00016 #include <sys/ipc.h> 00017 #include <sys/shm.h> 00018 #define SHMSZ 2048 00019 using namespace std; 00020 file_semaphore::file_semaphore(char* path_to_file) 00021 { 00022 00023 this->filepath+=path_to_file; 00024 this->resid=0; 00025 } 00026 void file_semaphore::getResource() 00027 { 00028 /* 00029 one_fs: 00030 srand ( time(NULL) ); 00031 if(!FileExists(this->filepath)){ 00032 //nyama takyv file. tryabva da syzdadem file-a 00033 char *buf33=new char[1000]; 00034 buf33[0]=0; 00035 strcat(buf33, MAINFOLDER); 00036 strcat(buf33, filepath.c_str()); 00037 two_fs: 00038 fp1=fopen(buf33, "w"); 00039 00040 if(!fp1){ 00041 usleep(200000+ rand()%800000); 00042 goto two_fs; 00043 } 00044 00045 00046 int i1=rand()%400+1; 00047 this->resid=i1; 00048 fprintf(fp1, "%d", i1); 00049 fclose(fp1); 00050 delete buf33; 00051 00052 00053 }else{ 00054 00055 usleep(200000+ rand()%800000); 00056 goto one_fs; 00057 } 00058 00059 00060 */ 00061 srand ( time(NULL) ); 00062 char c; 00063 int shmid; 00064 key_t key; 00065 char *shm, *s; 00066 00067 /* 00068 * We'll name our shared memory segment 00069 * "". 00070 */ 00071 key = hshcode(this->filepath); 00072 00073 /* 00074 * Create the segment or locate it. 00075 */ 00076 if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) { 00077 00078 exit(1); 00079 } 00080 00081 /* 00082 * Now we attach the segment to our data space. 00083 */ 00084 if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { 00085 00086 exit(1); 00087 } 00088 /* Inizializirane na shared memory, ako e syzdadena za 1 pyt*/ 00089 if(*shm!='*'){ 00090 *shm='*'; 00091 *(shm+1)='1'; 00092 s=shm+1; 00093 while(s-shm<SHMSZ){ 00094 *s++=0; 00095 00096 00097 } 00098 *(shm+1)=0; 00099 } 00100 //4akane da se osvobodi pametta ako nqkoj pi6e v neq 00101 while(*(shm+1)=='1'){ 00102 usleep(200000+ rand()%800000); 00103 00104 } 00105 //zaemame si pametta 00106 *(shm+1)='1'; 00107 int *t=shm; 00108 this->resid=getpid(); 00109 00110 00111 //narejdame se na opa6ka 00112 t++; 00113 while(*t!=0&&(char *)t<shm+SHMSZ*sizeof(int)){ 00114 t++; 00115 00116 } 00117 *t=this->resid; 00118 *(shm+1)=0; 00119 //4akame si reda na opa6kata 00120 while(*(((int *)shm)+1)!=this->resid){ 00121 usleep(200000+ rand()%800000); 00122 } 00123 00124 00125 00126 return; 00127 } 00128 void file_semaphore::freeResource() 00129 { 00130 /* 00131 char *buf33=new char[1000]; 00132 buf33[0]=0; 00133 strcat(buf33, MAINFOLDER); 00134 strcat(buf33, filepath.c_str()); 00135 fp1=fopen(buf33, "r"); 00136 if(fp1){ 00137 int t; 00138 fscanf(fp1, "%d", &t); 00139 fclose(fp1); 00140 if(t==this->resid) 00141 remove(filepath.c_str()); 00142 } 00143 delete buf33; 00144 */ 00145 srand ( time(NULL) ); 00146 char c; 00147 int shmid; 00148 key_t key; 00149 char *shm, *s; 00150 00151 /* 00152 * We'll name our shared memory segment 00153 * "". 00154 */ 00155 key = hshcode(this->filepath); 00156 00157 /* 00158 * Create the segment. 00159 */ 00160 if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) { 00161 00162 exit(1); 00163 } 00164 00165 /* 00166 * Now we attach the segment to our data space. 00167 */ 00168 if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { 00169 00170 exit(1); 00171 } 00172 //4akame nqkoj da se zapi6e na opa6kata, ako q e zael 00173 while(*(shm+1)=='1'){ 00174 usleep(50000+ rand()%500000); 00175 00176 } 00177 //zaemame si pametta 00178 *(shm+1)='1'; 00179 int *t=shm; 00180 t++; 00181 int *t1=t+1; 00182 //osvobojdavame opa6kata 00183 while(*t1||*t){ 00184 *t=*t1; 00185 t++; 00186 t1++; 00187 00188 } 00189 00190 //osvobojdavame si pametta 00191 *(shm+1)=0; 00192 return; 00193 00194 } 00195 file_semaphore::~file_semaphore() 00196 { 00197 /* 00198 if(fp1) 00199 fclose(fp1); 00200 */ 00201 } 00202