Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/mman.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <sys/time.h>
  9. #include <string.h>
  10.  
  11. int main(int argc, char **argv) {
  12.     struct stat st;
  13.     int fd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0600);
  14.     if (fd == -1) {
  15.         exit(1);
  16.     }
  17.     fstat(fd, &st);
  18.     int rows = strtol(argv[2], NULL, 10), colls = strtol(argv[3], NULL, 10);
  19.     int sz = rows * colls;
  20.     ftruncate(fd, sz*sizeof(int));
  21.     int *buf = mmap(NULL, sz*sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  22.     int i=0, j, k = 0, p=1;
  23.     while (i !=  sz) {
  24.         ++k;
  25.         for (j=k-1;j!= colls-k+1; ++j) {
  26.             buf[colls*(k-1)+j]=p++;
  27.             ++i;
  28.         }
  29.         for (j=k;j!= rows-k+1; ++j) {
  30.             buf[colls*(j)+colls-k]=p++;
  31.             ++i;
  32.         }
  33.         for (j=colls-k-1;j>=k-1; --j) {
  34.             buf[colls*(rows-k)+j]=p++;
  35.             ++i;
  36.         }
  37.         for (j=rows-k-1;j>=k; --j) {
  38.             buf[colls*(j)+k-1]=p++;
  39.             ++i;
  40.         }
  41.     }
  42.     munmap(buf, sz * sizeof(int));
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement