Virtlab:Linuxové okénko
Z VirtlabWiki
- seznam dobrych rad, osvedcenych postupu, a pod.
- Zaznam cinnosti na konzoli
- pokud delam praci pres konzoli(ssh) a chci to mit jako reprodukovatelny postup a zaroven jsem lenivy si vsechno psat co jsem udelal, tak existuje programek script ktery je schopen nahravat konzoli a k tomu je scriptreplay ktery umi nahravku prehrat
- programy v linuxu:
- script - nahrava konzoli (ulozi do souboru nahravku)
- scriptreplay - prehrava (cte ze souboru nahravku)
Psaní robustních shell skriptů
- Realizace kritické sekce v Bash
LOCKFILE=$SPOJ_DIR/spojovac.lock # Implementace kriticke sekce s pouzitim LOCKFILE # noclobber mode: redirection fails if (a regular) file to be written to exists if ( set -o noclobber; echo "$$" > "$LOCKFILE") 2> /dev/null; then trap 'rm -f "$LOCKFILE"; exit $?' INT TERM EXIT <CRITICAL SECTION> rm -f "$LOCKFILE" trap - INT TERM EXIT else echo "Failed to acquire LOCKFILE: $LOCKFILE." echo "Held by $(cat $LOCKFILE)" fi # Opsano z http://www.davidpashley.com/articles/writing-robust-shell-scripts.html
- MUTEX promocí UNIXových semaforů
Vytvoření, použití a smazání semaforu (spustit jako první)
#include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #include <stdio.h> #include <fcntl.h> extern int errno; int main(void) { struct sembuf sb; key_t key; int semid; if ((key=ftok ("/tmp/qqq", '1'))==-1) perror("FTOK:"); // zmenit soubor na neco rozumnejsiho z Virtlabu // create one semaphore with no special flags if ((semid = semget (key,1,IPC_CREAT| O_RDWR | 0777))==-1) perror("A:"); // set semaphore value to 1 sb.sem_num=0; sb.sem_op=1; sb.sem_flg=0; if (semop (semid, &sb,1)==-1) perror("B:"); // Wait until a mutex is released or proceed if it is free // Decrease value by 1 before proceeding sb.sem_num=0; sb.sem_op=-1; sb.sem_flg=0; printf("Waiting before semaphore\n"); if (semop (semid, &sb,1)==-1) perror("C:"); printf("Passed through semaphore. Waiting for input to release it.`\n"); getc(stdin); // release mutex - increase value by 1 sb.sem_num=0; sb.sem_op=1; sb.sem_flg=0; if (semop (semid, &sb,1)==-1) perror("D:"); printf("Semaphore released. Waiting for input to remove it\n"); getc(stdin); getc(stdin); // odstrani CR //remove the semaphore if (semctl(semid,1,IPC_RMID)==-1) perror("E:"); printf("Semaphore removed\n"); return 0; }
Synchronizace se semaforem (spustit poté)
- include <sys/types.h>
- include <sys/ipc.h>
- include <sys/sem.h>
- include <stdio.h>
- include <fcntl.h>
extern int errno;
int main(void) {
struct sembuf sb; key_t key; int semid;
if ((key=ftok ("/tmp/qqq", '1'))==-1) perror("FTOK:"); // zmenit soubor na neco rozumnejsiho z Virtlabu
// create one semaphore with no special flags if ((semid = semget (key,1, 0777))==-1) perror("A:");
// Wait until a mutex is released or proceed if it is free // Decrease value by 1 before proceeding
sb.sem_num=0;
sb.sem_op=-1;
sb.sem_flg=0;
printf("Waiting before semaphore\n");
if (semop (semid, &sb,1)==-1) perror("C:");
printf("Passed through semaphore. Waiting for input to release it.`\n");
getc(stdin);
// release mutex - increase value by 1 sb.sem_num=0; sb.sem_op=1; sb.sem_flg=0; if (semop (semid, &sb,1)==-1) perror("D:"); printf("Semaphore released\n"); return 0; }