Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/mman.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/time.h>
- #include <string.h>
- int main(int argc, char **argv) {
- struct stat st;
- int fd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0600);
- if (fd == -1) {
- exit(1);
- }
- fstat(fd, &st);
- int rows = strtol(argv[2], NULL, 10), colls = strtol(argv[3], NULL, 10);
- int sz = rows * colls;
- ftruncate(fd, sz*sizeof(int));
- int *buf = mmap(NULL, sz*sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
- int i=0, j, k = 0, p=1;
- while (i != sz) {
- ++k;
- for (j=k-1;j!= colls-k+1; ++j) {
- buf[colls*(k-1)+j]=p++;
- ++i;
- }
- for (j=k;j!= rows-k+1; ++j) {
- buf[colls*(j)+colls-k]=p++;
- ++i;
- }
- for (j=colls-k-1;j>=k-1; --j) {
- buf[colls*(rows-k)+j]=p++;
- ++i;
- }
- for (j=rows-k-1;j>=k; --j) {
- buf[colls*(j)+k-1]=p++;
- ++i;
- }
- }
- munmap(buf, sz * sizeof(int));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement