Monthly Archives: February 2016


C/C++: Pass random value from parent to child after fork() via a pipe()

The following code will create a pipe for each child, fork the process as many times as it is needed and send from the parent to each child a random int value, finally the children will read the value and terminate.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int main(int argc, char *argv[]) {
    int count = 3;
    int fd[count][2];
    int pid[count];
    srand(time(NULL));

    // create pipe descriptors
    for (int i = 0; i < count; i++) {
        pipe(fd[i]);
        // fork() returns 0 for child process, child-pid for parent process.
        pid[i] = fork();
        if (pid[i] != 0) {
            // parent: writing only, so close read-descriptor.
            close(fd[i][0]);

            // send the value on the write-descriptor.
            int r = rand();
            write(fd[i][1], &r, sizeof(r));
            printf("Parent(%d) send value: %d\n", getpid(), r);

            // close the write descriptor
            close(fd[i][1]);
        } else {
            // child: reading only, so close the write-descriptor
            close(fd[i][1]);

            // now read the data (will block)
            int id;
            read(fd[i][0], &id, sizeof(id));
            printf("%d Child(%d) received value: %d\n", i, getpid(), id);

            // close the read-descriptor
            close(fd[i][0]);
            //TODO cleanup fd that are not needed
            break;
        }
    }
    return 0;
}

C/C++: Pass value from parent to child after fork() via a pipe()

The following code will create a pipe, fork the process and then send from the parent to the child an int value (the id we want to give to the child), finally the child will read the value and terminate.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
	int fd[2];
	int childID = 0;

	// create pipe descriptors
	pipe(fd);

	// fork() returns 0 for child process, child-pid for parent process.
	if (fork() != 0) {
		// parent: writing only, so close read-descriptor.
		close(fd[0]);

		// send the childID on the write-descriptor.
		childID = 1;
		write(fd[1], &childID, sizeof(childID));
		printf("Parent(%d) send childID: %d\n", getpid(), childID);

		// close the write descriptor
		close(fd[1]);
	} else {
		// child: reading only, so close the write-descriptor
		close(fd[1]);

		// now read the data (will block until it succeeds)
		read(fd[0], &childID, sizeof(childID));
		printf("Child(%d) received childID: %d\n", getpid(), childID);

		// close the read-descriptor
		close(fd[0]);
	}
	return 0;
}

ffmpeg: Extract audio from .MP4 to .MP3

The following command will find all mp4 files that are in the current directory and in all sub-folders and extract the audio to mp3 format.

find . -type f -iname "*.mp4" -exec bash -c 'FILE="$1"; ffmpeg -i "${FILE}" -vn -y "${FILE%.mp4}.mp3";' _ '{}' \;

The filename of the audio file will be the same as the mp4 video with the correct extension. The mp4 extension will be removed and replaced by the mp3 extension e.g hi.mp4 will become hi.mp3


Contest Management System (CMS) How to change the ranking system logo

To replace the Ranking page logo:
logo
In case you installed the ContestCMS (version 1.2.0) already, you will find the image here:
/usr/local/lib/python2.7/dist-packages/cms-1.2.0-py2.7.egg/cmsranking/static/img/logo.png
If not, assuming you downloaded the project code to the folder cms:
cms/cmsranking/static/img/logo.png
Your new picture should be 200 pixels wide and 160 pixels tall.

mysqldump: Got error: 1044: Access denied for user ‘username’@’ip’ to database ‘db’ when doing LOCK TABLES

When trying to issue the command

mysqldump -h "mysql.example.com" -u "username" -p"password" db table_A table_B

to get the MySQL dump for two tables (table_A and table_B), you will get the error

mysqldump: Got error: 1044: Access denied for user 'username'@'ip' to database 'db' when doing LOCK TABLES

when your account does not have the ‘LOCK TABLES’ right.

The ‘LOCK TABLES’ right gives you access to the command LOCK that can be used to emulate transactions or to get more speed when updating tables by preventing other sessions from modifying the locked tables.

To mitigate the problem, use the --single-transaction option as follows

mysqldump -h "mysql.example.com" -u "username" -p"password" --single-transaction db table_A table_B

 

Documentation:

·   --single-transaction

This option sets the transaction isolation mode to REPEATABLE READ and sends a START TRANSACTION SQL statement to the server before dumping data. It is useful only with transactional tables such as InnoDB, because then it dumps the consistent state of the database at the time when START TRANSACTION was issued without blocking any applications.

When using this option, you should keep in mind that only InnoDB tables are dumped in a consistent state. For example, any MyISAM or MEMORY tables dumped while using this option may still change state.

While a --single-transaction dump is in process, to ensure a valid dump file (correct table contents and binary log coordinates), no other connection should use the following statements: ALTER TABLE, CREATE TABLE, DROP TABLE, RENAME TABLE, TRUNCATE TABLE. A consistent read is not isolated from those statements, so use of them on a table to be dumped can cause the SELECT that is performed by mysqldump to retrieve the table contents to obtain incorrect contents or fail.

The --single-transaction option and the --lock-tables option are mutually exclusive because LOCK TABLES causes any pending transactions to be committed implicitly.

This option is not supported for MySQL Cluster tables; the results cannot be guaranteed to be consistent due to the fact that the NDBCLUSTER storage engine supports only the READ_COMMITTED transaction isolation level. You should always use NDB backup and restore instead.

To dump large tables, combine the --single-transaction option with the --quick option.

 

 


2GB επιπλέον χώρος στο GMail

Για να πάρετε δωρεάν 2GB επιπλέον χώρο μπείτε στον έλεγχο ασφαλείας του profile σας μέχρι τις 11 Φεβρουαρίου 2016 (αύριο) και ολοκληρώστε την διαδικασία.

Διαρκεί μόνο 2 λεπτά και 2GB επιπλέον χώρος θα γίνουν δικά σας!

Αυτή η κίνηση έγινε επ’ευκαιρίας της ημέρας ασφαλής χρήσης του διαδικτύου.


Ημερίδα Πληροφορικής 2016

Impress/ PowerPoint format Download: Προοπτικές Καριέρας Τεχνολογίας Πληροφοριών και Επικοινωνίας [PowerPoint / Impress] (88 downloads)

PDF format Download: Προοπτικές Καριέρας Τεχνολογίας Πληροφοριών και Επικοινωνίας [PDF] (129 downloads)

7η Ημερίδα Πληροφορικής για μαθητές/τριες Λυκείων και Τεχνικών Σχολών

Ο βασικός στόχος της Ημερίδας Πληροφορικής είναι να ενημερώσει, να διαφωτίσει, αλλά κυρίως να ενθαρρύνει τους νέους μας να ανακαλύψουν τις ευκαιρίες εκπαίδευσης στην Επιστήμη της Πληροφορικής καθώς και να πληροφορηθούν για τις επαγγελματικές προοπτικές που προσφέρει η επιστήμη αυτή. Η ημερίδα στοχεύει κυρίως στους μαθητές της Α’ τάξης Λυκείων και Τεχνικών Σχολών, οι οποίοι βρίσκονται στο κρίσιμο στάδιο της
επιλογής των επόμενων μεταλυκειακών βημάτων τους. Η ημερίδα ευελπιστεί ότι θα βοηθήσει τους συμμετέχοντες να αποκτήσουν μια καλύτερη άποψη για την Επιστήμη της Πληροφορικής. Την προσπάθειά μας αυτή ενισχύει το Υπουργείο Παιδείας και Πολιτισμού καθώς και ο Κυπριακός Σύνδεσμος Πληροφορικής (Cyprus Computer Society – CCS).

hmerida060216

Impress/ PowerPoint format Download: Προοπτικές Καριέρας Τεχνολογίας Πληροφοριών και Επικοινωνίας [PowerPoint / Impress] (88 downloads)

PDF format Download: Προοπτικές Καριέρας Τεχνολογίας Πληροφοριών και Επικοινωνίας [PDF] (129 downloads)

 


[GitLab.com] Clone all repositories in your account

GitLab.com offers a public API that allows us to get information related to our accounts. One of the API calls available is the account projects call (http://gitlab.com/api/v3/projects).

This call will return a JSON object describing the projects available to your account.

To clone all of the projects available to you, you can use the following:

TOKEN="PASTE_YOUR_PRIVATE_TOKEN_HERE"; PREFIX="ssh_url_to_repo"; curl --header "PRIVATE-TOKEN: $TOKEN" http://gitlab.com/api/v3/projects | grep -o "\"$PREFIX\":[^ ,]\+" | awk -F ':' '{printf "ssh://"; for (i=2; i<NF; i++) printf $i "/"; print $NF}' | xargs -L1 git clone

The above code will bring the JSON object, filter out everything except for the “ssh_url_to_repo” member of each project and then it will use it to clone the project by fixing up the URL to be used by git.

To get the above code working: the GitLab API requires that you use a token that is related to your account instead of using your credentials to make the call to the API.

To get your private token, visit this page http://gitlab.com/profile/account , the private token is the random sequence of characters in the white box:

[GitLab.com] Private TokenYou need to copy that value in the place of the variable TOKEN in the above script.

In case you have a lot of projects (more than 10), the default call will only produce the results for the first 10 repositories only.

To list all available repositories you have two options:

  1.  Set the per_page query parameter to a value big enough to fetch all your projects information if they are less than 100. e.g http://gitlab.com/api/v3/projects?per_page=100
  2. Follow the link headers from the initial response to make all the next calls.

[GitLab.com] Get a list with the names of all repositories in your account

GitLab.com offers a public API that allows us to get information related to our accounts. One of the API calls available is the account projects call (http://gitlab.com/api/v3/projects).

This call will return a JSON object describing the projects available to your account.

To get a list of the names of the projects available to you, you can use the following:

TOKEN="PASTE_YOUR_PRIVATE_TOKEN_HERE"; PREFIX="ssh_url_to_repo"; curl --header "PRIVATE-TOKEN: $TOKEN" http://gitlab.com/api/v3/projects | grep -o "\"$PREFIX\":[^ ,]\+" | xargs -L1 basename | awk -F '.' '{print $1}'

The above code will bring the JSON object, filter out everything except for the “ssh_url_to_repo” member of each project and then it will print it out on screen.

 

To get the above code working: the GitLab API requires that you use a token that is related to your account instead of using your credentials to make the call to the API.

To get your private token, visit this page http://gitlab.com/profile/account , the private token is the random sequence of characters in the white box:

[GitLab.com] Private TokenYou need to copy that value in the place of the variable TOKEN in the above script.

 

In case you have a lot of projects (more than 10), the default call will only produce the results for the first 10 repositories only.

To list all available repositories you have two options:

  1.  Set the per_page query parameter to a value big enough to fetch all your projects information if they are less than 100. e.g http://gitlab.com/api/v3/projects?per_page=100
  2. Follow the link headers from the initial response to make all the next calls.